在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):SalomonBrys/Kotson开源软件地址(OpenSource Url):https://github.com/SalomonBrys/Kotson开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):DEPRECATEDThis library is deprecated. You can still use it. It is not going any where. However, it is not maintained anymore. If you are looking for a cool, up-to-date JSON parsing library for Kotlin/JVM: here you go! Kotson: Gson for KotlinKotson enables you to parse and write JSON with Google's Gson using a conciser and easier syntax. Kotson is a set of extension functions, meaning that it adds utility functions and syntactic sugars to Gson in Kotlin. It does not add new features to Gson nor does it creates new types. It is therefore usable on any Gson object, whether created from java or kotlin in source or library. InstallMaven:
Gradle:
Table Of ContentsUsageCreating JsonJsonObject: import com.github.salomonbrys.kotson.*
val obj: JsonObject = jsonObject(
"name" to "kotson",
"creation" to Date().getTime(),
"files" to 4
) JsonArray: import com.github.salomonbrys.kotson.*
val arr: JsonArray = jsonArray("one", "two", 42, 21.5) Of course, a combination of both: import com.github.salomonbrys.kotson.*
val obj: JsonObject = jsonObject(
"property" to "value",
"array" to jsonArray(
21,
"fourty-two",
jsonObject("go" to "hell")
)
) JsonPrimitives: import com.github.salomonbrys.kotson.*
val pi = 42.toJson() // java: new JsonPrimitive(42);
val pf = 42.21f.toJson() // java: new JsonPrimitive(42.21f);
val pd = 42.21.toJson() // java: new JsonPrimitive(42.21d);
val pc = 'c'.toJson() // java: new JsonPrimitive('c');
val pz = true.toJson() // java: new JsonPrimitive(true);
val os = "coucou".toJson() // java: new JsonPrimitive("coucou"); Setting custom (de)serializersIf you need to register a import com.github.salomonbrys.kotson.*
val gson = GsonBuilder()
.registerTypeAdapter<MyType> {
serialize {
/*
it.type: Type to serialize from
it.context: GSon context
it.src : Object to serialize
*/
}
deserialize {
/*
it.type: Type to deserialize to
it.context: GSon context
it.json : JsonElement to deserialize from
*/
}
createInstances {
/*
it: Type of instance to create
*/
}
}
.registerTypeHierarchyAdapter<MyOtherType> {
serialize { /* Same a above */ }
deserialize { /* Same a above */ }
createInstances { /* Same a above */ }
}
.create() Of course, you can declare only a serializer, a deserializer or an instance creator. You don't have to declare all three. You don't have to declare all your //TypeAdapters.kt
import com.github.salomonbrys.kotson.*
val personSerializer = jsonSerializer { /* Same arguments as before */ } //Main.kt
import com.github.salomonbrys.kotson.*
val gson = GsonBuilder().registerTypeAdapter<Person>(personSerializer).create() Setting custom Readers and WritersGson has another API (named Stream API) that allows to register writers (to import com.github.salomonbrys.kotson.*
val gson = GsonBuilder()
.registerTypeAdapter<Person> {
write {
beginArray()
value(it.name)
value(it.age)
endArray()
}
read {
beginArray()
val name = nextString()
val age = nextInt()
endArray()
Person(name, age)
}
}
.create() While a bit more complex and difficult to handle, this API is also better optimized. So if you're after performance, I recommend you use this API. Using this API has a few drawbacks:
If you wish to register a nullable reader or writer, you can use You don't have to declare all your //TypeAdapters.kt
import com.github.salomonbrys.kotson.*
val personTypeAdapter = typeAdapter<Person> {
write { /*...*/ }
read { /*...*/ }
} //Main.kt
import com.github.salomonbrys.kotson.*
val gson = GsonBuilder().registerTypeAdapter<Person>(personSerializer).create() Kotson provides no utility function for the Parsing JSONKotson provides a simple API that does not suffer from Java's type erasure. That means that whatever the output type, it will be parsed correctly and eliminates the need for import com.github.salomonbrys.kotson.*
val gson = Gson()
// java: List<User> list = gson.fromJson(src, new TypeToken<List<User>>(){}.getType());
val list1 = gson.fromJson<List<User>>(jsonString)
val list2 = gson.fromJson<List<User>>(jsonElement)
val list3 = gson.fromJson<List<User>>(jsonReader)
val list4 = gson.fromJson<List<User>>(reader) Attention: A lot of Gson's APIs are relying on Careful: the Browsing Json ElementsKotson allows you to simply convert a jsonElement to a primitive, a import com.github.salomonbrys.kotson.*
val s = json.string // java: String s = json.getAsString();
val i = json.int // java: int i = json.getAsInt();
val a = json.array // java: JsonArray = json.getAsJsonArray();
val o = json.obj // java: JsonObject = json.getAsJsonObject();
val ns = json.nullString // java: String s = json.isJsonNull() ? null : json.getAsString();
val ni = json.nullInt // java: Integer i = json.isJsonNull() ? null : json.getAsInt();
val na = json.nullArray // java: JsonArray = json.isJsonNull() ? null : json.getAsJsonArray();
val no = json.nullObj // java: JsonObject = json.isJsonNull() ? null : json.getAsJsonObject(); The same APIs exist for Kotson provides a simple API that allows you to easily browse import com.github.salomonbrys.kotson.*
// java: JsonElement components = colors.getAsJsonObject().get("orange");
val components = colors["orange"]
// java: JsonElement greenComp = components.getAsJsonArray().get(1);
val greenComp = components[1]
// java: int greenComp = json .getAsJsonObject()
// .getAsJsonObject("colors")
// .getAsJsonArray("orange")
// .get(1)
// .getAsInt();
val greenComp = json["colors"]["orange"][1].int Mutating Json ElementsKotson allows you to mutate a import com.github.salomonbrys.kotson.*
fun test() {
val array = jsonArray("zero", "x", "two")
array[1] = "one"
array += "three"
array -= "zero"
val obj = jsonObject()
obj["this"] = "that"
obj += "answer" to 42
obj -= "this"
} Copying Json ElementsKotson allows you to make a shallow copy (single-level copy) or a deep copy (recursive copy) of a import com.github.salomonbrys.kotson.*
val shallow = json.shallowCopy()
val deep = json.deepCopy() Accessing object fields via property delegatesKotson allows you to delegate properties to import com.github.salomonbrys.kotson.*
class Person(public val obj: JsonObject) {
val id: String by obj.byString // Maps to obj["id"]
val name: String by obj.byString("fullName") // Maps to obj["fullName"]
val birthDate: Int by obj["dates"].byInt(0) // Maps to obj["dates"][0]
} Let's talk!You've read so far ?! You're awsome! Why don't you drop by the Kotson Slack channel on Kotlin's Slack group? |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论