There is some legacy Java pojos which was used for binary serialization. Among the fields of one pojo, I have one enum field. Now in the new Java pojo, the enum field is replaced by a string field.
// old pojo with enum
class Test {
private DataType dataType;
private String someOtherField;
}
enum DataType {
Int,
Float,
String
}
// new pojo without enum field
class NewTest {
private String dataType;
private String someOtherField;
}
While reading(de-serializing) the old data, I have used the techniques mentioned here - https://stackoverflow.com/a/14608062/314310 to read old data into the new refactored pojo, which performs successfully for the non enum fields. But reading enum data to string field is almost seems impossible. I am getting exception as
java.lang.ClassCastException: cannot assign instance of demo.DataType to field demo.NewTest.dataType of type java.lang.String in instance of demo.NewTest
Is there anyway I can achieve this?
EDIT:
Here is the code for my custom ObjectInputStream
class MyObjectInputStream extends ObjectInputStream {
private static final Map<String, Class<?>> migrationMap = new HashMap<>();
static {
migrationMap.put("demo.Test", NewTest.class);
migrationMap.put("demo.DataType", String.class);
}
public MyObjectInputStream(InputStream stream) throws IOException {
super(stream);
}
#Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
ObjectStreamClass resultClassDescriptor = super.readClassDescriptor();
for (final String oldName : migrationMap.keySet()) {
if (resultClassDescriptor != null && resultClassDescriptor.getName().equals(oldName)) {
Class<?> replacement = migrationMap.get(oldName);
try {
resultClassDescriptor = ObjectStreamClass.lookup(replacement);
} catch (Exception e) {
log.error("Error while replacing class name." + e.getMessage(), e);
}
}
}
return resultClassDescriptor;
}
}
Try changing your enum into this:
enum DataType {
Int,
Float,
String;
public static DataType getFromString(String stringDataType) {
for(DataType dataType in DataType.values()) {
if (dataType.toString().equals(stringDataType)) {
return dataType;
}
}
throw new IllegalArgumentException("Invalid input");
}
}
So when you want to assign the Enum to String you call:
newTest.dataType = test.dataType.toString();
And when you want to assign the String to Enum, you call:
test.dataType = DataType.getFromString(newTest.dataType);
Related
Is it possible to configure jackson to serialize simple value objects, wrapping only one attribute, to be serialized like enums?
public final class ErrorCode {
private final String value;
public ErrorCode(#JsonProperty("value") final String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
Now it is serialized as ..., "errorCode":{"value":"invalid.login"}, ...
but I would like to have, as if it were enum, ..., "errorCode":"invalid.login", ....
It is possible to but only via #JsonUnwrapped() in each surrounding class
SorroundingClass {
#JsonUnwrapped()
private ErrorCode errorCode;
...
}
I would like to configure It only in one place, best in ErrorCode itself.
Looking at flattening-nested-attributes-in-jackson it seems to me impossible, but I want to make sure.
You can achieve this by implementing custom com.fasterxml.jackson.databind.JsonSerializer serializer:
class SingleValueJsonSerializer extends JsonSerializer<Object> {
#Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value == null) {
gen.writeNull();
return;
}
final Field[] fields = value.getClass().getDeclaredFields();
if (fields.length != 1) {
throw new IOException("Do not use this serialiser for the class " + value.getClass().getSimpleName() + " which has" + fields.length + " fields!");
}
final Field first = fields[0];
try {
final Object fieldValue = first.get(value);
gen.writeObject(fieldValue);
} catch (IllegalAccessException e) {
throw new IOException(e);
}
}
}
You can define it in required class:
class Wrapper {
#JsonSerialize(using = SingleValueJsonSerializer.class)
private ErrorCode error;
// other fields
}
or register it globally by defining on the class level:
#JsonSerialize(using = SingleValueJsonSerializer.class)
class ErrorCode {
Using GSON in Java is there any annotation where I can indicate a field that it should keep it as a raw string even though it is an object. ?
Or What would be the easiest way to achieve this?
//This is the original
#SerializedName("perro")
public Perro perro
//This is what I want to achieve
#SerializedName("perro")
public String perro
So the result should be
perro = "{"Users":[{"Name":"firulais","Raza":"beagle"},{"Name":"Spike","Value":"Terrier"}]}"
The only way I found this to work was using
public JsonElement perro;
Based on #mrsegev's answer, here's a simpler version (in Kotlin) that works with arbitrary objects:
class RawJsonAdapter: TypeAdapter<String>() {
override fun write(out: JsonWriter?, value: String?) {
out?.jsonValue(value)
}
override fun read(reader: JsonReader?): String {
return JsonParser().parse(reader).toString()
}
}
This takes advantage of JsonWriter#jsonValue() which was added in https://github.com/google/gson/pull/667
Usage:
#JsonAdapter(RawJsonAdapter::class)
val fieldName: String? = null
Basically speaking, You need to create a custom gson TypeAdapter class and write the conversion login from Object to String yourself.
Then annotate the field indicating what TypeAdapter to use in order to read/write it using gson.
More details in this blog post: Gson TypeAdapter Example
Example: Prasing class object as a raw JSON string
public class StringTypeAdapter extends TypeAdapter<String> {
#Override
public void write(JsonWriter out, String value) throws IOException {
try {
JSONObject jsonObject = new JSONObject(value);
out.beginObject();
Iterator<String> iterator = jsonObject.keys();
while (iterator.hasNext()) {
String key = iterator.next();
String keyValue = jsonObject.getString(key);
out.name(key).value(keyValue);
}
out.endObject();
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public String read(JsonReader in) throws IOException {
in.beginObject();
JSONObject jsonObject = new JSONObject();
while (in.hasNext()) {
final String name = in.nextName();
final String value = in.nextString();
try {
jsonObject.put(name, value);
} catch (JSONException e) {
e.printStackTrace();
}
}
in.endObject();
return jsonObject.toString();
}
}
Using the TypeAdapter:
#JsonAdapter(StringTypeAdapter.class)
private String someClass; // Lazy parsing this json
You should be able to use public JsonObject perro;
You can then call gson.toJson(perro) to get the String value.
I want to make a Map (String ,Object) like this
{AssessmentId=0, Physical_name='ram', Physical_height=20, Physical_weight=60}
from my Pojo Class - InitialAssessment
public class InitialAssessment {
private long AssessmentId;
private String physical_name;
private String physical_gender;
private int physical_height;
private float physical_weight;
// all getter And setter is Created here
}
without using any external Library like Gson etc.
You can use this approach:
public Map getMapFromPojo(InitialAssessment assessment) throws Exception {
Map<String, Object> map = new HashMap<>();
if (assessment != null) {
Method[] methods = assessment.getClass().getMethods();
for (Method method : methods) {
String name = method.getName();
if (name.startsWith("get") && !name.equalsIgnoreCase("getClass")) {
Object value = "";
try {
value = method.invoke(assessment);
map.put(name.substring(name.indexOf("get") + 3), value);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return map;
}
return null;
}
It will give you map for pojo class like this:
Output:
{AssessmentId=0, Physical_name='ram', Physical_gender='Male' , Physical_height=20, Physical_weight=60}
I'm using retrofit with gson to deserialize my json into realm objects. This works very well for the most part. Trouble arises when dealing with
RealmList(String(or any other basic data type))
Since Realm doesnt support RealmList where E doesnt extend Realm object, I wrapped String in a RealmObject.
public class RealmString extends RealmObject {
private String val;
public String getValue() {
return val;
}
public void setValue(String value) {
this.val = value;
}
}
My realm Object is as below
public class RealmPerson extends RealmObject {
#PrimaryKey
private String userId;
...
private RealmList<RealmString> stringStuff;
private RealmList<SimpleRealmObj> otherStuff;
<setters and getters>
}
SimpleRealmObj works fine as it only has String elements
public class SimpleRealmObj extends RealmObject {
private String foo;
private String bar;
...
}
How can I deserialize stringStuff? I tried using a gson TypeAdapter
public class RealmPersonAdapter extends TypeAdapter<RealmPerson> {
#Override
public void write(JsonWriter out, RealmPerson value) throws IOException {
out.beginObject();
Log.e("DBG " + value.getLastName(), "");
out.endObject();
}
#Override
public RealmPerson read(JsonReader in) throws IOException {
QLRealmPerson rList = new RealmPerson();
in.beginObject();
while (in.hasNext()) {
Log.e("DBG " + in.nextString(), "");
}
in.endObject();
return rList;
}
However I still hit the IllegalStateException
2334-2334/com.qualcomm.qlearn.app E//PersonService.java:71﹕ main com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was NAME at line 1 column 3 path $.
I tried RealmList, RealmString adapter earlier to no avail.
The only workaround I managed to find so far is https://github.com/realm/realm-java/issues/620#issuecomment-66640786
Any better options?
It is better to use JsonSerializer and JsonDeserializer rather than TypeAdapter for your RealmObject, because of 2 reasons:
They allow you to delegate (de)serialization for your RealmObject to the default Gson (de)serializer, which means you don't need to write the boilerplate yourself.
There's a weird bug in Gson 2.3.1 that might cause a StackOverflowError during deserialization (I tried the TypeAdapter approach myself and encountered this bug).
Here's how (replace Tag with your RealmObject class):
(NOTE that context.serialize and context.deserialize below are equivalent to gson.toJson and gson.fromJson, which means we don't need to parse the Tag class ourselves.)
Parser + serializer for RealmList<Tag>:
public class TagRealmListConverter implements JsonSerializer<RealmList<Tag>>,
JsonDeserializer<RealmList<Tag>> {
#Override
public JsonElement serialize(RealmList<Tag> src, Type typeOfSrc,
JsonSerializationContext context) {
JsonArray ja = new JsonArray();
for (Tag tag : src) {
ja.add(context.serialize(tag));
}
return ja;
}
#Override
public RealmList<Tag> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
RealmList<Tag> tags = new RealmList<>();
JsonArray ja = json.getAsJsonArray();
for (JsonElement je : ja) {
tags.add((Tag) context.deserialize(je, Tag.class));
}
return tags;
}
}
Tag class:
#RealmClass
public class Tag extends RealmObject {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Then register your converter class with Gson:
Gson gson = new GsonBuilder()
.registerTypeAdapter(new TypeToken<RealmList<Tag>>() {}.getType(),
new TagRealmListConverter())
.create();
The error message "Expected a string but was NAME" can be solved by retrieving the name of the json object in the JsonReader before the actual json object (which is a String in your case).
You can take a look at the Android documentation for JsonReader. It has detailed explanation and code snippet. You can also take a look at the readMessage method in the sample code snippet in the documentation.
I have modified your read method to what I think it should be. NOTE: I didn't test the code, so there may be some minor errors in it.
#Override
public RealmPerson read(JsonReader in) throws IOException {
RealmPerson rList = new RealmPerson();
in.beginObject();
String name = "";
while (in.hasNext()) {
name = in.nextName();
if (name.equals("userId")) {
String userId = in.nextString();
// update rList here
} else if (name.equals("otherStuff")) {
// since otherStuff is a RealmList of RealmStrings,
// your json data would be an array
// You would need to loop through the array to retrieve
// the json objects
in.beginArray();
while (in.hasNext()) {
// begin each object in the array
in.beginObject();
name = in.nextName();
// the RealmString object has just one property called "value"
// (according to the code snippet in your question)
if (name.equals("val")) {
String val = in.nextString();
// update rList here
} else {
in.skipValue();
}
in.endObject();
}
in.endArray();
} else {
in.skipValue();
}
}
in.endObject();
return rList;
}
Let me know if this helps.
My gson typeAdapter was the culprit.
The above error was seen as I wasnt deserializing the json into RealmPerson correctly, the first field is not a String, hence
in.nextString()
was borking.
I looked at some example code and it hit me, I didnt have to use
in.beginObject() and in.endObject()
to deserialize a String. The below code works.
public class QLRealmStringAdapter extends TypeAdapter<QLRealmString> {
#Override
public void write(JsonWriter out, QLRealmString value) throws IOException {
Log.e("DBG " + value.getValue(), "");
out.value(value.getValue());
}
#Override
public RealmString read(JsonReader in) throws IOException {
RealmString rString = new RealmString();
if (in.hasNext()) {
String nextStr = in.nextString();
System.out.println("DBG " + nextStr);
rString.setValue(nextStr);
}
return rString;
}
}
Hope this helps someone.
i need a jackson serializer and deserializer for the Converting Arraylist to RealmList
This question already has answers here:
gson: Treat null as empty String
(6 answers)
Closed 3 years ago.
I'm using gson to serialize some object. I have a requirement that any null field should be treated as an empty string, independent on the variable being a string, double or a pojo.
I tried to create a custom serializer for Object and simply return a new JsonPrimitive("") for the null valued objects, but the problem is how to handle the non-null valued objects without the use of "instanceof" or "getClass" and handling every single type.
Any thoughts on how to do this is appreciated.
This can be done using a custom TypeAdaptor for your model Object.
You can iterate over the object field using reflection and whenever a field is null, set the value in the json representation to an empty string whenever you cross a null field.
This would absolutely be hard to maintain and should be done with some risks as #Sotirios Delimanolis stated, What if the corresponding reference is not a String, how are you going to intending to handle it back and forth?
Here is a bean structure just used to showcase the situation:
public class MyEntity
{
private int id;
private String name;
private Long socialNumber;
private MyInnerEntity innerEntity;
public MyEntity(int id, String name, Long socialNumber, MyInnerEntity innerEntity)
{
this.id = id;
this.name = name;
this.socialNumber = socialNumber;
this.innerEntity = innerEntity;
}
public int getId()
{
return id;
}
public String getName()
{
return name;
}
public Long getSocialNumber()
{
return socialNumber;
}
public MyInnerEntity getInnerEntity()
{
return innerEntity;
}
public static class MyInnerEntity {
#Override
public String toString()
{
return "whateverValue";
}
}
}
Here is the TypeAdapter implementation which set any null value to and empty "" String:
public class GenericAdapter extends TypeAdapter<Object>
{
#Override
public void write(JsonWriter jsonWriter, Object o) throws IOException
{
jsonWriter.beginObject();
for (Field field : o.getClass().getDeclaredFields())
{
Object fieldValue = runGetter(field, o);
jsonWriter.name(field.getName());
if (fieldValue == null)
{
jsonWriter.value("");
}
else {
jsonWriter.value(fieldValue.toString());
}
}
jsonWriter.endObject();
}
#Override
public Object read(JsonReader jsonReader) throws IOException
{
/* Don't forget to add implementation here to have your Object back alive :) */
return null;
}
/**
* A generic field accessor runner.
* Run the right getter on the field to get its value.
* #param field
* #param o {#code Object}
* #return
*/
public static Object runGetter(Field field, Object o)
{
// MZ: Find the correct method
for (Method method : o.getClass().getMethods())
{
if ((method.getName().startsWith("get")) && (method.getName().length() == (field.getName().length() + 3)))
{
if (method.getName().toLowerCase().endsWith(field.getName().toLowerCase()))
{
try
{
return method.invoke(o);
}
catch (IllegalAccessException e)
{ }
catch (InvocationTargetException e)
{ }
}
}
}
return null;
}
}
Now a simple straightforward main method to add the adapter to Gson:
public class Test
{
public static void main(String[] args)
{
Gson gson = new GsonBuilder().registerTypeAdapter(MyEntity.class, new GenericAdapter()).create();
Object entity = new MyEntity(16, "entity", (long)123, new MyEntity.MyInnerEntity());
String json = gson.toJson(entity);
System.out.println(json);
Object entity2 = new MyEntity(0, null, null, null);
json = gson.toJson(entity2);
System.out.println(json);
}
}
This would result in below output:
{"id":"16","name":"entity","socialNumber":"123","innerEntity":"whateverValue"}
{"id":"0","name":"","socialNumber":"","innerEntity":""}
Note that whatever the object is of type, its value is set to "" in the marshalled json string.
See gson: Treat null as empty String