jakiej
11/02/2018, 12:10 AM{
"a": {
"b": "c"
}
}
to
@Serializable
class Wrapper(val a: Map<String, Any> = mapOf())
what's the easiest way? (if there a way I can specify default (e.g, LinkedHashMap
) for Map and default (String
) for "c"?sandwwraith
11/02/2018, 11:44 AMclass Wrapper(val a: Map<String, Any> = linkedMapOf("b" to "c"))
?jakiej
11/02/2018, 3:40 PM{ "a": { "b": "c" }}
to Wrapper
with ease. The default (de)serializer assumes type information is also present: { "a": { "b": ["kotlin.String", "c"] }}
.sandwwraith
11/02/2018, 8:37 PMAny
as type parameter. Can you replace it with String
?jakiej
11/03/2018, 5:02 AMAny
. could be a String
, another Map
or a List
.@Serializable class Wrapper(val a: Map<String, CharSequence> = mapOf())
will serialize to { "a": { "b": "c" }}
too, but deserialization does not work either.sandwwraith
11/06/2018, 9:14 AMJsonTreeMapper
to parse untyped structure to a generic json treejakiej
11/06/2018, 4:49 PMJsonTreeMapper
is not really a choice, and I really need to deserialize the JSON into its binding. Is there a way to specify a default type for certain JSON node?sandwwraith
11/07/2018, 4:17 PM{ "a": { "b": ["kotlin.String", "c"] }}
. So the problem here is only when you're integrating with other JSON forms of type recording.
Deserializing values into Any
without type information is indeed prohibited by design: kotlinx.serialization has pull model, where you first determine the type, then look into the input stream, and if the type mismatches, it's an error.jakiej
11/07/2018, 5:36 PM@Serializable class Wrapper(val a: Map<String, java.io.Serializable>)
can be serialized to a JSON string, but the same JSON string can NOT be deserialized.a: Map<String, java.io.Serializable>
sandwwraith
11/12/2018, 5:07 PMjava.io.Serializable
relatedjakiej
11/12/2018, 5:12 PMa: Map<String, java.io.Serializable>
(or it can be relaxed to a: Map<String, Any>
). with java.io.Serializable
, this field can be serialized to "a": { "b": "c" }
, but can't be deserialized. with Any
, it can be deserialized, but the serialization has type info (`"a": { "b": [String, "c"] }) with is not backward compatible.