2

hi everyone i wrote a Deserialize code has shown below

public class JsonDataDeserialisation implements JsonDeserializer<JsonData>{

    @Override
    public JsonData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        // TODO Auto-generated method stub
        JsonObject jobj = json.getAsJsonObject();
        String jsontype = jobj.get("type").getAsString();
        String jsonname = jobj.get("name").getAsString();
        String jsondoc = jobj.get("doc").getAsString();
        Fields[] fields = context.deserialize(jobj.get("fields"), Fields[].class);
        JsonData jdata = new JsonData();
        jdata.setType(jsontype);
        jdata.setName(jsonname);
        jdata.setDoc(jsondoc);
        jdata.setFields(fields);
        return jdata;
    }

}

for the json event given.

{ "type": "record", "name": "Doc", "doc": "adoc", fields: [{ "name": "id", "type": "string" }] }

but when it comes to multiple objects in an fields array like

{ "type": "record", "name": "Doc", "doc": "adoc", fields: [{ "name": "id", "type": "string" }, { "name": "id1", "type": "string1" },: { "name": "id2", "type": "string2" }] }

i tried this code and it show me the

Exception in thread "main" java.lang.UnsupportedOperationException: JsonObject error

public class JsonDataDeserialisation implements JsonDeserializer<JsonData>{

    @Override
    public JsonData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        // TODO Auto-generated method stub
        JsonObject jobj = json.getAsJsonObject();
        String jsontype = jobj.get("type").getAsString();
        String jsonname = jobj.get("name").getAsString();
        String jsondoc = jobj.get("doc").getAsString();

        Fields[] fields = context.deserialize(jobj.get("fields"), Fields[].class);
        JsonArray fields1 =jobj.get("fields").getAsJsonArray();
        String[] fieldst = new String[fields1.size()];
        for(int i=0;i<fieldst.length;i++){
            JsonElement jsonfields = fields1.get(i);
            fieldst[i] = jsonfields.getAsString();
        }
        JsonData jdata = new JsonData();
        jdata.setType(jsontype);
        jdata.setName(jsonname);
        jdata.setDoc(jsondoc);
        jdata.setFields(fields);
        return jdata;
    }

}

where am i going wrong please help me out thankyou

Jonathan Portorreal
  • 2,730
  • 4
  • 21
  • 38
raghu
  • 43
  • 4

0 Answers0