• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

cbeust/klaxon: A JSON parser for Kotlin

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

cbeust/klaxon

开源软件地址(OpenSource Url):

https://github.com/cbeust/klaxon

开源编程语言(OpenSource Language):

Kotlin 99.9%

开源软件介绍(OpenSource Introduction):

Klaxon logo

Klaxon is a library to parse JSON in Kotlin

Install

dependencies {
    implementation 'com.beust:klaxon:5.5'
}

Community

Join the #klaxon Slack channel.

Use

Klaxon has different API's depending on your needs:

These four API's cover various scenarios and you can decide which one to use based on whether you want to stream your document and whether you need to query it.

Streaming Query Manipulation
Object binding API No No Kotlin objects
Streaming API Yes No Kotlin objects and JsonObject/JsonArray
Low level API No Yes Kotlin objects
JSON Path query API Yes Yes JsonObject/JsonArray

Object binding API

General usage

To use Klaxon's high level API, you define your objects inside a class. Klaxon supports all the classes you can define in Kotlin:

  • Regular and data classes.
  • Mutable and immutable classes.
  • Classes with default parameters.

For example:

class Person(val name: String, val age: Int)

Classes with default parameters are supported as well:

class Person (val name: String, var age: Int = 23)

Once you've specified your value class, you invoke the parse() function, parameterized with that class:

val result = Klaxon()
    .parse<Person>("""
    {
      "name": "John Smith",
    }
    """)

assert(result?.name == "John Smith")
assert(result.age == 23)

The @Json annotation

The @Json annotation allows you to customize how the mapping between your JSON documents and your Kotlin objects is performed. It supports the following attributes:

name

Use the name attribute when your Kotlin property has a different name than the field found in your JSON document:

data class Person(
    @Json(name = "the_name")
    val name: String
)
val result = Klaxon()
    .parse<Person>("""
    {
      "the_name": "John Smith", // note the field name
      "age": 23
    }
""")

assert(result.name == "John Smith")
assert(result.age == 23)

ignored

You can set this boolean attribute to true if you want certain properties of your value class not to be mapped during the JSON parsing process. This is useful if you defined additional properties in your value classes.

class Ignored(val name: String) {
   @Json(ignored = true)
   val actualName: String get() = ...
}

In this example, Klaxon will not try to find a field called actualName in your JSON document.

Note that you can achieve the same result by declaring these properties private:

class Ignored(val name: String) {
   private val actualName: String get() = ...
}

Additionally, if you want to declare a property private but still want that property to be visible to Klaxon, you can annotate it with @Json(ignored = false).

index

The index attribute allows you to specify where in the JSON string the key should appear. This allows you to specify that certain keys should appear before others:

class Data(
    @Json(index = 1) val id: String,
    @Json(index = 2) val name: String
)
println(Klaxon().toJsonString(Data("id", "foo")))

// displays { "id": "id", "name": "foo" }

whereas

class Data(
    @Json(index = 2) val id: String,
    @Json(index = 1) val name: String
)
println(Klaxon().toJsonString(Data("id", "foo")))

// displays { "name": "foo" , "id": "id" }

Properties that are not assigned an index will be displayed in a non deterministic order in the output JSON.

serializeNull

By default, all properties with the value null are serialized to JSON, for example:

class Data(
    val id: Int?
)
println(Klaxon().toJsonString(Data(null)))

// displays { "id": null }

If you instead want the properties with a null value to be absent in the JSON string, use @Json(serializeNull = false):

class Data(
    @Json(serializeNull = false)
    val id: Int?
)
println(Klaxon().toJsonString(Data(null)))

// displays {}

If serializeNull is false, the Kotlin default values for this property will be ignored during parsing. Instead, if the property is absent in the JSON, the value will default to null.

If you don't want to apply this option to every attribute, you can also set it as an instance-wide setting for Klaxon:

val settings = KlaxonSettings(serializeNull = false)

This saves you the hassle of setting these attributes onto every single field.

data class User(
    val username: String, val email: String, // mandatory
    val phone: String?, val fax: String?, val age: Int? // optional
)

Klaxon(settings)
  .toJsonString(User("user", "[email protected]", null, null, null))

// displays {}

You may still set settings with the @Json annotation onto specific fields. They will take precedence over global settings of the Klaxon instance.

Renaming fields

On top of using the @Json(name=...) annotation to rename fields, you can implement a field renamer yourself that will be applied to all the fields that Klaxon encounters, both to and from JSON. You achieve this result by passing an implementation of the FieldRenamer interface to your Klaxon object:

    val renamer = object: FieldRenamer {
        override fun toJson(fieldName: String) = FieldRenamer.camelToUnderscores(fieldName)
        override fun fromJson(fieldName: String) = FieldRenamer.underscoreToCamel(fieldName)
    }

    val klaxon = Klaxon().fieldRenamer(renamer)

Custom types

Klaxon will do its best to initialize the objects with what it found in the JSON document but you can take control of this mapping yourself by defining type converters.

The converter interface is as follows:

interface Converter {
    fun canConvert(cls: Class<*>) : Boolean
    fun toJson(value: Any): String
    fun fromJson(jv: JsonValue) : Any
}

You define a class that implements this interface and implement the logic that converts your class to and from JSON. For example, suppose you receive a JSON document with a field that can either be a 0 or a 1 and you want to convert that field into your own type that's initialized with a boolean:

class BooleanHolder(val flag: Boolean)

val myConverter = object: Converter {
    override fun canConvert(cls: Class<*>)
        = cls == BooleanHolder::class.java

    override fun toJson(value: Any): String
        = """{"flag" : "${if ((value as BooleanHolder).flag == true) 1 else 0}"""

    override fun fromJson(jv: JsonValue)
        = BooleanHolder(jv.objInt("flag") != 0)

}

Next, you declare your converter to your Klaxon object before parsing:

val result = Klaxon()
    .converter(myConverter)
    .parse<BooleanHolder>("""
        { "flag" : 1 }
    """)

assert(result.flag)

JsonValue

The Converter type passes you an instance of the JsonValue class. This class is a container of a Json value. It is guaranteed to contain one and exactly one of either a number, a string, a character, a JsonObject or a JsonArray. If one of these fields is set, the others are guaranteed to be null. Inspect that value in your converter to make sure that the value you are expecting is present, otherwise, you can cast a KlaxonException to report the invalid JSON that you just found.

Field conversion overriding

It's sometimes useful to be able to specify a type conversion for a specific field without that conversion applying to all types of your document (for example, your JSON document might contain various dates of different formats). You can use field conversion types for this kind of situation.

Such fields are specified by your own annotation, which you need to specify as targetting a FIELD:

@Target(AnnotationTarget.FIELD)
annotation class KlaxonDate

Next, annotate the field that requires this specific handling in the constructor of your class. Do note that such a constructor needs to be annotated with @JvmOverloads:

class WithDate @JvmOverloads constructor(
    @KlaxonDate
    val date: LocalDateTime
)

Define your type converter (which has the same type as the converters defined previously). In this case, we are converting a String from JSON into a LocalDateTime:

val dateConverter = object: Converter {
    override fun canConvert(cls: Class<*>)
        = cls == LocalDateTime::class.java

    override fun fromJson(jv: JsonValue) =
        if (jv.string != null) {
            LocalDateTime.parse(jv.string, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))
        } else {
            throw KlaxonException("Couldn't parse date: ${jv.string}")
        }

    override fun toJson(o: Any)
            = """ { "date" : $o } """
}

Finally, declare the association between that converter and your annotation in your Klaxon object before parsing:

val result = Klaxon()
    .fieldConverter(KlaxonDate::class, dateConverter)
    .parse<WithDate>("""
    {
      "theDate": "2017-05-10 16:30"
    }
""")

assert(result?.date == LocalDateTime.of(2017, 5, 10, 16, 30))

Property strategy

You can instruct Klaxon to dynamically ignore properties with the PropertyStrategy interface:

interface PropertyStrategy {
    /**
     * @return true if this property should be mapped.
     */
    fun accept(property: KProperty<*>): Boolean
}

This is a dynamic version of @Json(ignored = true), which you can register with your Klaxon instance with the function propertyStrategy():

val ps = object: PropertyStrategy {
    override fun accept(property: KProperty<*>) = property.name != "something"
}
val klaxon = Klaxon().propertyStrategy(ps)

You can define multiple PropertyStrategy instances, and in such a case, they all need to return true for a property to be included.

Polymorphism

JSON documents sometimes contain dynamic payloads whose type can vary. Klaxon supports two different use cases for polymorphism: polymorphic classes and polymorphic fields. Klaxon gives you control on polymorphism with the annotation @TypeFor, which can be placed either on a class or on a field.

Polymorphic classes

A polymorphic class is a class whose actual type is defined by one of its own properties. Consider this JSON:

[
    { "type": "rectangle", "width": 100, "height": 50 },
    { "type": "circle", "radius": 20}
]

The content of the field type determines the class that needs to be instantiated. You would model this as follows with Klaxon:

@TypeFor(field = "type", adapter = ShapeTypeAdapter::class)
open class Shape(val type: String)
data class Rectangle(val width: Int, val height: Int): Shape("rectangle")
data class Circle(val radius: Int): Shape("circle")

The type adapter is as follows:

class ShapeTypeAdapter: TypeAdapter<Shape> {
    override fun classFor(type: Any): KClass<out Shape> = when(type as String) {
        "rectangle" -> Rectangle::class
        "circle" -> Circle::class
        else -> throw IllegalArgumentException("Unknown type: $type")
    }
}

Polymorphic fields

Klaxon also allows a field to determine the class to be instantiated for another field. Consider the following JSON document:

[
    { "type": 1, "shape": { "width": 100, "height": 50 } },
    { "type": 2, "shape": { "radius": 20} }
]

This is an array of polymorphic objects. The type field is a discriminant which determines the type of the field shape: if its value is 1, the shape is a rectangle, if 2, it's a circle.

To parse this document with Klaxon, we first model these classes with a hierarchy:

open class Shape
data class Rectangle(val width: Int, val height: Int): Shape()
data class Circle(val radius: Int): Shape()

We then define the class that the objects of this array are instances of:

class Data (
    @TypeFor(field = "shape", adapter = ShapeTypeAdapter::class)
    val type: Integer,

    val shape: Shape
)

Notice the @TypeFor annotation, which tells Klaxon which field this value is a discriminant for, and also provides a class that will translate these integer values into the correct class:

class ShapeTypeAdapter: TypeAdapter<Shape> {
    override fun classFor(type: Any): KClass<out Shape> = when(type as Int) {
        1 -> Rectangle::class
        2 -> Circle::class
        else -> throw IllegalArgumentException("Unknown type: $type")
    }
}

With this code in place, you can now parse the provided JSON document above the regular way and the the following tests will pass:

val shapes = Klaxon().parseArray<Data>(json)
assertThat(shapes!![0].shape as Rectangle).isEqualTo(Rectangle(100, 50))
assertThat(shapes[1].shape as Circle).isEqualTo(Circle(20))

Streaming API

The streaming API is useful in a few scenarios:

  • When your JSON document is very large and reading it all in memory might cause issues.
  • When you want your code to react as soon as JSON values are being read, without waiting for the entire document to be parsed.

This second point is especially important to make mobile apps as responsive as possible and make them less reliant on network speed.

Note: the streaming API requires that each value in the document be handled by the reader. If you are simply looking to extract a single value the PathMatcher API may be a better fit.

Writing JSON with the streaming API

As opposed to conventional JSON libraries, Klaxon doesn't supply a JsonWriter class to create JSON documents since this need is already covered by the json() function, documented in the Advanced DSL section.

Reading JSON with the streaming API

Streaming JSON is performed with the JsonReader class. Here is an example:

val objectString = """{
     "name" : "Joe",
     "age" : 23,
     "flag" : true,
     "array" : [1, 3],
     "obj1" : { "a" : 1, "b" : 2 }
}"""

JsonReader(StringReader(objectString)).use { reader ->
    reader.beginObject() {
        var name: String? = null
        var age: Int? = null
        var flag: Boolean? = null
        var array: List<Any> = arrayListOf<Any>()
        var obj1: JsonObject? = null
        while (reader.hasNext()) {
            val readName = reader.nextName()
            when (readName) {
                "name" -> name = reader.nextString()
                "age" -> age = reader.nextInt()
                "flag" -> flag = reader.nextBoolean()
                "array" -> array = reader.nextArray()
                "obj1" -> obj1 = reader.nextObject()
                else -> Assert.fail("Unexpected name: $readName")
            }
        }
    }
}

There are two special functions to be aware of: beginObject() and beginArray(). Use these functions when you are about to read an object or an array from your JSON stream. These functions will make sure that the stream is correctly positioned (open brace or open bracket) and once you are done consuming the content of that entity, the functions will make sure that your object is correctly closed (closing brace or closing bracket). Note that these functions accept a closure as an argument, so there are no closeObject()/closeArray() functions.

It is possible to mix both the object binding and streaming API's, so you can benefit from the best of both worlds.

For example, suppose your JSON document contains an array with thousands of elements in them, each of these elements being an object in your code base. You can use the streaming API to consume the array one element at a time and then use the object binding API to easily map these elements directly to one of your objects:

data class Person(val name: String, val age: Int)
val array = """[
        { "name": "Joe", "age": 23 },
        { "name": "Jill", "age": 35 }
    ]"""

fun streamingArray() {
    val klaxon = Klaxon()
    JsonReader(StringReader(array)).use { reader ->
        val result = arrayListOf<Person>()
        reader.beginArray {
            while (reader.hasNext()) {
                val person = klaxon.parse<Person1>(reader)
                result.add(person)
            }
        }
    }
}

JSON Path Query API

The JSON Path specification defines how to locate elements inside a JSON document. Klaxon allows you to define path matchers that can match specific elements in your document and receive a callback each time a matching element is found.

Consi


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap