JSON to Java Objects, best practice for modeling the json stream

后端 未结 3 1354
心在旅途
心在旅途 2021-01-20 17:58

I have a JSON stream being generated by a server side C++ program that is currently in development. I\'ve been given a sample of the resulting JSON and I am concerned that I

相关标签:
3条回答
  • 2021-01-20 18:31

    I'd be more concerned with what looks like metadata fields in the data stream. The top level 'abort' and 'more' attributes look like some kind of structured string which you may have to parse? Aside from that, you just need to model each Java object with the widest possible set of fields that will be sent from your external program. You don't have to worry if transmitted data has one or more of the fields missing, most JSON libraries will just deserialize a null in that case. Also, most of the JSON libraries will also allow you to specify that you want to ignore unknown incoming fields.

    So between deserializing null for missing fields and ignoring extra fields, you should be good to go for your parse.

    0 讨论(0)
  • 2021-01-20 18:38

    No this isn't particularly hard to deserialize into Java objects. JSON doesn't have many clues about typing information other than their primitives. Most of the concern about loosing type information can be recovered either by looking at the object you want to deserialize into, or have the user provide the object they want to deserialize into. That's precisely how flexjson works, and you have a lot of flexibility to define at each point how to deserialize it. It comes with sensible defaults for most problems, but you can always attach an ObjectFactory to specific class or path with in the JSON stream.

    http://flexjson.sourceforge.net

    0 讨论(0)
  • 2021-01-20 18:40

    With GSON, assuming that the class you are deserializing into has fields for all the names that appear in the JSON, the fields not found in the JSON will just be left null:

    https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects

    "While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null"

    Things get a little more complicated if arbitrary field names are allowed in the JSON - for example, if Point allows c1, c2, ... cn. But you can handle this with a custom deserializer.

    https://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Deserializer

    Edit:

    Here's how you might write a custom deserializer for Point:

    private class DateTimeDeserializer implements JsonDeserializer<Point> {
        public Point deserialize(JsonElement json, Type typeOfT, 
                JsonDeserializationContext context) throws JsonParseException {
            List<PointPart> parts = Lists.newArrayList();
    
            for(Map.Entry<String,JsonElement> entry : 
                    json.getAsJsonObject().entrySet()) {
                char type = ;
                int index = Integer.parseInt(entry.getKey().substring(1)) - 1;
    
                while(parts.size() <= index) {
                    parts.add(new PointPart());
                }
    
                PointPart part = parts.get(index);
                switch(entry.getKey().charAt(0)) {
                case 'c':
                    part.c = entry.getValue().getAsBoolean();
                    break;
                case 'k':
                    part.k = entry.getValue().getAsInt();
                    break;
                }
            }
    
            return new Point(parts);
        }
    }
    
    class Point {
        List<PointPart> parts;
    
        Point(List<PointPart> parts) {
            this.parts = parts;
        }
    }
    
    class PointPart {
        boolean c;
        int k;
    }
    
    0 讨论(0)
提交回复
热议问题