GSON equivalent for readerforupdating in Jackson - jackson

Any GSON equivalent for readerforupdating in Jackson?
ObjectMapper mapper = new ObjectMapper();
mapper.readerForUpdating(obj).readValue("{}");

Related

JsonProperty names are not being respected on Jackson

I would like to have custom names when writing my object but it's not printing what I have defined in annotation #JsonProperty:
data class Banking(
#JsonProperty("personita_id")
val clientId: String?,
#JsonProperty("contita_id")
val accountId: String?
)
val mapper = ObjectMapper()
val xpto = mapper.writeValueAsString(Banking("mammamia", "miaccuenta"))
xpto prints:
{"clientId": "mammamia", "accountId": "miaccuenta"}
What is missing in my code?
You can register the KotlinModule on the ObjectMapper from jackson-module-kotlin
val mapper = ObjectMapper().registerModule(KotlinModule())
or depending on the used version
val mapper = ObjectMapper().registerKotlinModule();

can not serialize data class in data class with objectmapper and kotlin

Can not serialize data class in data class with kotlin and objectmapper
I'am using kotlin version "1.3.72" and
com.fasterxml.jackson.module:jackson-module-kotlin version "2.11.2".
This is my classes
data class A(val b: B)
data class B(val name: String)
val a = A(B(name = "name")
objectMapper.writeValueAsString(a) // << failed!!!
Why couldn't serialize?

Implement object by delegation in Kotlin

It's possible to implement class by delegation
class Envs(
val map: Map<String, String> = to_map()
) : Map<String, String> by map
But it doesn't work for object, the code below won't compile
object Envs : Map<String, String> by map {
val map: Map<String, String> = to_map()
}
As also explained in the comments, object cannot take parameters, since they are initialized statically (in static block of java).
And for the updated question, it is not possible to point to a variable which is not declared in primary constructor. The same goes for the class:
class Envs : Map<String, String> by map {
val map: Map<String, String> = to_map()
}
^ The above code won't compile, because map variable can't be accessed at the time you are delegating. It is only available to the methods declared inside it.
You can do what you want with:
object Envs : Map<String, String> by to_map()
Or if you want to have a reference of the map, while you can't but since the object is Map itself, you can hold its reference.
object Envs : Map<String, String> by to_map() {
val map: Map<String, String> = this
}
But it (^) is kinda useless, you can just use this or Envs to access the Map instead.

Jackson ObjectMapper ignore a given class

I have this bean
class Toto
{
private final String fuu;
private final String bar;
Private List<Dog> dogs;
}
class Dog {
private String name;
private String race;
}
I want to convert this bean into a Map<> using:
ObjectMapper serializer = new ObjectMapper();
serializer.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return serializer.convertValue(entry, Map.class);
This code result is that Dog objects are also converted into a Map.
I want a solution so that in the final map i would have:
fuu=fuu_value_as_String
bar=bar_value_as_String
dogs=[ObjectDog1, ObjectDog2] // i do not want the Object to be
converted (because i have an instanceof check later in the code)
Thanks

Deserialize gson Object with nested List<Object>

public class ListResult<T> {
private boolean ok;
private String message;
private java.util.List<T> data;
private Paging paging;
}
When i want to deserialize JSON given form RESTful call like this :
Type fooType = new TypeToken<ListResult<T>>() {}.getType();
Gson gson = new Gson();
Object model = gson.fromJson(strResult, fooType);
A get A ListResult where data field is a list of StringMap instead a List of T like defined in ListResult class
Any idea ?
Type fooType = new TypeToken<ListResult<StringMap>>() {}.getType();
Gson gson = new Gson();
ListResult<StringMap> model = gson.fromJson(strResult, fooType);
Gson can't read your mind, or determine a Generic type all on its own ;)
https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Generic-Types
Just don't use a list, use an array MyObj[] myObjs. You don't need to use the TypeToken way