Renetik Android - Json

38903 단어 androidjsonlibrary

https://github.com/renetik/renetik-android-json ➜ 문서



읽기 쉬운 코드를 작성하면서 애플리케이션 개발을 즐기고, 개선하고, 속도를 높일 수 있는 프레임워크입니다.
많은 프로젝트에서 라이브러리로 사용되며 새로운 프로젝트를 개발하면서 개선합니다. 나는 열려있다
Hire 또는 내 모바일 앱 음악 제작 및 공연에 대한 투자
프로젝트 Renetik Instrumentswww.renetik.com .

allprojects {
    repositories {
        maven { url 'https://github.com/renetik/maven-snapshot/raw/master/repository' } //for master-SNAPSHOT
        maven { url 'https://github.com/renetik/maven/raw/master/repository' } 
    }
}


2단계. 종속성 추가

dependencies {
    implementation 'com.renetik.library:renetik-android-json:$renetik-android-verison'
}





data class SimpleJsonObject(
    var string: String? = null,
    var map: Map<String, Boolean>? = null,
    var list: List<Double>? = null) : CSJsonObject() {

    init {
        set("stringKey", string)
        set("mapKey", map)
        set("listKey", list)
    }

    override fun onLoaded() {
        string = get("stringKey")
        map = getBooleanMap("mapKey", default = false)
        list = getDoubleList("listKey", default = 0.0)
    }
}

@RunWith(RobolectricTestRunner::class)
class SimpleJsonObjectTest {
    private val exceptedJson: String = """{
  "stringKey": "testObject",
  "mapKey": {
    "key1": true,
    "key2": false
  },
  "listKey": [
    1.2,
    3.4,
    5
  ]
}"""

    @Test
    fun customJsonObjectSetGetTest2() {
        val instance = SimpleJsonObject("testObject",
            mapOf("key1" to true, "key2" to false),
            listOf(1.2, 3.4, 5.0))
        assertEquals(exceptedJson, instance.toJson(formatted = true))
        assertEquals(instance, SimpleJsonObject().load(exceptedJson))
    }
}



data class ComplexJsonObject(
    var string: String? = null,
    var map: Map<String, ComplexJsonObject>? = null,
    var list: List<ComplexJsonObject>? = null) : CSJsonObject() {

    init {
        string?.let { set("stringKey", it) }
        map?.let { set("mapKey", it) }
        list?.let { set("listKey", it) }
    }

    override fun onLoaded() {
        string = get("stringKey")
        map = getJsonObjectMap("mapKey", ComplexJsonObject::class)
        list = getJsonObjectList("listKey", ComplexJsonObject::class)
    }
}

@RunWith(RobolectricTestRunner::class)
class ComplexJsonObjectTest {
    private val expectedJson = """
{
  "stringKey": "testObject",
  "mapKey": {
    "mapKey1": {
      "stringKey": "mapTestObject1"
    },
    "mapKey2": {
      "stringKey": "mapTestObject2"
    }
  },
  "listKey": [
    {
      "stringKey": "listTestObject1"
    },
    {
      "stringKey": "listTestObject2"
    }
  ]
}""".trimStart()

    @Test
    fun customJsonObjectSetGetTest2() {
        val instance = ComplexJsonObject("testObject",
            mapOf("mapKey1" to ComplexJsonObject("mapTestObject1"),
                "mapKey2" to ComplexJsonObject("mapTestObject2")),
            listOf(ComplexJsonObject("listTestObject1"), ComplexJsonObject("listTestObject2")))

        Assert.assertEquals(expectedJson, instance.toJson(formatted = true))
        Assert.assertEquals(instance, ComplexJsonObject().load(expectedJson))
    }
}



class JsonObjectTest {
    @Test
    fun jsonObjectStoreString() {
        val json = CSJsonObject().apply { set("key", "some string") }.toJson()
        assertEquals("""{"key":"some string"}""", json)
        val value = CSJsonObject(json).getString("key")
        assertEquals(value, "some string")
    }

    @Test
    fun jsonObjectStoreBooleanAsString() {
        forceStringInJson = true
        val json = CSJsonObject().apply { set("key", false) }.toJson()
        assertEquals("""{"key":"false"}""", json)
        val value = CSJsonObject(json).getBoolean("key")
        assertEquals(value, false)
    }

    @Test
    fun jsonObjectStoreBoolean() {
        forceStringInJson = false
        val json = CSJsonObject().apply { set("key", false) }.toJson()
        assertEquals("""{"key":false}""", json)
        val value = CSJsonObject(json).getBoolean("key")
        assertEquals(value, false)
    }

    @Test
    fun jsonObjectStoreInt() {
        forceStringInJson = true
        val json = CSJsonObject().apply { set("key", int = 345) }.toJson()
        assertEquals("""{"key":"345"}""", json)
        val value = CSJsonObject(json).getInt("key")
        assertEquals(value, 345)
    }

    @Test
    fun jsonObjectStoreDouble() {
        forceStringInJson = true
        val json = CSJsonObject().apply { set("key", 213131.131331) }.toJson()
        assertEquals("""{"key":"213131.131331"}""", json)
        val value = CSJsonObject(json).getDouble("key")
        assertEquals(value, 213131.131331)
    }

    @Test
    fun jsonObjectStoreListString() {
        val value = listOf("1", "2", "3")
        val json = CSJsonObject().apply { set("key", value) }.toJson()
        assertEquals("""{"key":["1","2","3"]}""", json)
        val jsonObject = CSJsonObject(json)
        assertEquals(value, jsonObject.getStringList("key"))
        assertEquals(listOf(1, 2, 3), jsonObject.getIntList("key"))
    }

    @Test
    fun jsonObjectStoreListFloat() {
        forceStringInJson = false
        val value: List<Float> = listOf(1f, 2.5f, 32349.89f)
        val json = CSJsonObject().apply { set("key", value) }.toJson()
        assertEquals("""{"key":[1,2.5,32349.89]}""", json)
        val jsonObject = CSJsonObject(json)
        assertEquals(value, jsonObject.getFloatList("key"))
        assertEquals(listOf("1", "2.5", "32349.89"), jsonObject.getStringList("key"))
    }

    @Test
    fun jsonObjectStoreListDoubleAsString() {
        forceStringInJson = true
        val value: List<Double> = listOf(1.0, 2.5, 32349.89)
        val json = CSJsonObject().apply { set("key", value) }.toJson()
        assertEquals("""{"key":["1.0","2.5","32349.89"]}""", json)
        val jsonObject = CSJsonObject(json)
        assertEquals(value, jsonObject.getDoubleList("key"))
        assertEquals(listOf("1.0", "2.5", "32349.89"), jsonObject.getStringList("key"))
    }

    @Test
    fun jsonObjectStoreListDouble() {
        forceStringInJson = false
        val value: List<Double> = listOf(1.0, 2.5, 32349.89)
        val json = CSJsonObject().apply { set("key", value) }.toJson()
        assertEquals("""{"key":[1,2.5,32349.89]}""", json)
        val jsonObject = CSJsonObject(json)
        assertEquals(value, jsonObject.getDoubleList("key"))
        assertEquals(listOf("1", "2.5", "32349.89"), jsonObject.getStringList("key"))
    }

    @Test
    fun jsonObjectStoreMapString() {
        forceStringInJson = false
        val value: Map<String, String> =
            mapOf("key1" to "value1", "key2" to "value2", "key3" to "value3")
        val json = CSJsonObject().apply { set("key", value) }.toJson()
        assertEquals("""{"key":{"key1":"value1","key2":"value2","key3":"value3"}}""", json)
        val returnValue = CSJsonObject(json).getStringMap("key")
        assertEquals(value, returnValue)
    }

    @Test
    fun jsonObjectStoreIntMapToStringMap() {
        forceStringInJson = false
        val value: Map<String, Double> = mapOf("key1" to 1.2, "key2" to 2.3, "key3" to 3.4)
        val json = CSJsonObject().apply { set("key", value) }.toJson()
        assertEquals("""{"key":{"key1":1.2,"key2":2.3,"key3":3.4}}""", json)
        val jsonObject = CSJsonObject(json)
        assertEquals(mapOf("key1" to "1.2", "key2" to "2.3", "key3" to "3.4"),
            jsonObject.getStringMap("key"))
        assertEquals(mapOf("key1" to 1.2f, "key2" to 2.3f, "key3" to 3.4f),
            jsonObject.getFloatMap("key"))
    }

    @Test
    fun jsonObjectStoreIntMapForceString() {
        forceStringInJson = false
        val value: Map<String, Double> = mapOf("key1" to 1.2, "key2" to 2.3, "key3" to 3.4)
        val json = CSJsonObject().apply { set("key", value) }.toJson()
        assertEquals("""{"key":{"key1":1.2,"key2":2.3,"key3":3.4}}""", json)
        val jsonObject = CSJsonObject(json)
        assertEquals(mapOf("key1" to "1.2", "key2" to "2.3", "key3" to "3.4"),
            jsonObject.getStringMap("key"))
        assertEquals(mapOf("key1" to 1.2f, "key2" to 2.3f, "key3" to 3.4f),
            jsonObject.getFloatMap("key"))
    }
}


Renetik Android - 라이브러리



https://github.com/renetik/renetik-android-core ➜ 문서



https://github.com/renetik/renetik-android-json ➜ 문서



https://github.com/renetik/renetik-android-event ➜ 문서



https://github.com/renetik/renetik-android-store ➜ 문서



https://github.com/renetik/renetik-android-preset ➜ 문서



https://github.com/renetik/renetik-android-framework ➜ 문서

좋은 웹페이지 즐겨찾기