tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.16k stars 1.03k forks source link

Failed mapping numeric json components to pojo #1074

Open nurettn opened 5 years ago

nurettn commented 5 years ago

Your JSON dictionary:

{
    "near_earth_objects": {
        "2019-09-05": [
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3843599?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3843599",
                "neo_reference_id": "3843599",
                "name": "(2019 QL1)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3843599",
                "absolute_magnitude_h": 22.959,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.0680385386,
                        "estimated_diameter_max": 0.1521387973
                    },
                    "meters": {
                        "estimated_diameter_min": 68.0385385653,
                        "estimated_diameter_max": 152.1387973218
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0422771747,
                        "estimated_diameter_max": 0.0945346366
                    },
                    "feet": {
                        "estimated_diameter_min": 223.2235588666,
                        "estimated_diameter_max": 499.1430518053
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 04:06",
                        "epoch_date_close_approach": 1567656360000,
                        "relative_velocity": {
                            "kilometers_per_second": "2.8310679829",
                            "kilometers_per_hour": "10191.8447385526",
                            "miles_per_hour": "6332.815971019"
                        },
                        "miss_distance": {
                            "astronomical": "0.2446249668",
                            "lunar": "95.1591120852",
                            "kilometers": "36595373.982100716",
                            "miles": "22739310.9566172408"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3843554?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3843554",
                "neo_reference_id": "3843554",
                "name": "(2019 PA3)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3843554",
                "absolute_magnitude_h": 21.196,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.1532339429,
                        "estimated_diameter_max": 0.3426415127
                    },
                    "meters": {
                        "estimated_diameter_min": 153.23394288,
                        "estimated_diameter_max": 342.6415127401
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0952151283,
                        "estimated_diameter_max": 0.2129074994
                    },
                    "feet": {
                        "estimated_diameter_min": 502.7360491585,
                        "estimated_diameter_max": 1124.1519806581
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 15:14",
                        "epoch_date_close_approach": 1567696440000,
                        "relative_velocity": {
                            "kilometers_per_second": "10.7701650701",
                            "kilometers_per_hour": "38772.594252405",
                            "miles_per_hour": "24091.7822453347"
                        },
                        "miss_distance": {
                            "astronomical": "0.4110251775",
                            "lunar": "159.8887940475",
                            "kilometers": "61488491.070371925",
                            "miles": "38207176.660805565"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/2237805?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "2237805",
                "neo_reference_id": "2237805",
                "name": "237805 (2002 CF26)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2237805",
                "absolute_magnitude_h": 17.4,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.8801465209,
                        "estimated_diameter_max": 1.9680674509
                    },
                    "meters": {
                        "estimated_diameter_min": 880.1465209007,
                        "estimated_diameter_max": 1968.067450894
                    },
                    "miles": {
                        "estimated_diameter_min": 0.5468975238,
                        "estimated_diameter_max": 1.22290004
                    },
                    "feet": {
                        "estimated_diameter_min": 2887.6199116319,
                        "estimated_diameter_max": 6456.914415591
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 08:40",
                        "epoch_date_close_approach": 1567672800000,
                        "relative_velocity": {
                            "kilometers_per_second": "19.6401174898",
                            "kilometers_per_hour": "70704.42296333",
                            "miles_per_hour": "43932.9788129647"
                        },
                        "miss_distance": {
                            "astronomical": "0.1025450852",
                            "lunar": "39.8900381428",
                            "kilometers": "15340526.324888524",
                            "miles": "9532161.0461046712"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3747987?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3747987",
                "neo_reference_id": "3747987",
                "name": "(2016 FD1)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3747987",
                "absolute_magnitude_h": 23.1,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.063760979,
                        "estimated_diameter_max": 0.1425738833
                    },
                    "meters": {
                        "estimated_diameter_min": 63.7609789875,
                        "estimated_diameter_max": 142.5738833281
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0396192233,
                        "estimated_diameter_max": 0.0885912765
                    },
                    "feet": {
                        "estimated_diameter_min": 209.1895703015,
                        "estimated_diameter_max": 467.7620993781
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 00:55",
                        "epoch_date_close_approach": 1567644900000,
                        "relative_velocity": {
                            "kilometers_per_second": "26.8497370503",
                            "kilometers_per_hour": "96659.0533809997",
                            "miles_per_hour": "60060.1768077662"
                        },
                        "miss_distance": {
                            "astronomical": "0.4209059692",
                            "lunar": "163.7324220188",
                            "kilometers": "62966636.462605604",
                            "miles": "39125653.6171971752"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3826850?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3826850",
                "neo_reference_id": "3826850",
                "name": "(2018 PU23)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3826850",
                "absolute_magnitude_h": 28.1,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.0063760979,
                        "estimated_diameter_max": 0.0142573883
                    },
                    "meters": {
                        "estimated_diameter_min": 6.3760978988,
                        "estimated_diameter_max": 14.2573883328
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0039619223,
                        "estimated_diameter_max": 0.0088591276
                    },
                    "feet": {
                        "estimated_diameter_min": 20.9189570301,
                        "estimated_diameter_max": 46.7762099378
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 06:54",
                        "epoch_date_close_approach": 1567666440000,
                        "relative_velocity": {
                            "kilometers_per_second": "10.8984036813",
                            "kilometers_per_hour": "39234.2532528341",
                            "miles_per_hour": "24378.6391947957"
                        },
                        "miss_distance": {
                            "astronomical": "0.3066200625",
                            "lunar": "119.2752043125",
                            "kilometers": "45869708.249266875",
                            "miles": "28502115.045457875"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3747357?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3747357",
                "neo_reference_id": "3747357",
                "name": "(2016 EL158)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3747357",
                "absolute_magnitude_h": 20.4,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.2210828104,
                        "estimated_diameter_max": 0.4943561926
                    },
                    "meters": {
                        "estimated_diameter_min": 221.0828103591,
                        "estimated_diameter_max": 494.3561926196
                    },
                    "miles": {
                        "estimated_diameter_min": 0.137374447,
                        "estimated_diameter_max": 0.3071786018
                    },
                    "feet": {
                        "estimated_diameter_min": 725.3373275385,
                        "estimated_diameter_max": 1621.9035709942
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 20:27",
                        "epoch_date_close_approach": 1567715220000,
                        "relative_velocity": {
                            "kilometers_per_second": "22.604595531",
                            "kilometers_per_hour": "81376.5439114679",
                            "miles_per_hour": "50564.2197432118"
                        },
                        "miss_distance": {
                            "astronomical": "0.2725107983",
                            "lunar": "106.0067005387",
                            "kilometers": "40767034.977679621",
                            "miles": "25331460.8996799298"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/2459200?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "2459200",
                "neo_reference_id": "2459200",
                "name": "459200 (2012 DK61)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2459200",
                "absolute_magnitude_h": 21.0,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.1677084622,
                        "estimated_diameter_max": 0.3750075218
                    },
                    "meters": {
                        "estimated_diameter_min": 167.7084621628,
                        "estimated_diameter_max": 375.0075217981
                    },
                    "miles": {
                        "estimated_diameter_min": 0.1042091748,
                        "estimated_diameter_max": 0.2330187988
                    },
                    "feet": {
                        "estimated_diameter_min": 550.2246310023,
                        "estimated_diameter_max": 1230.3396778159
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 11:35",
                        "epoch_date_close_approach": 1567683300000,
                        "relative_velocity": {
                            "kilometers_per_second": "3.3138917068",
                            "kilometers_per_hour": "11930.0101445103",
                            "miles_per_hour": "7412.8443589598"
                        },
                        "miss_distance": {
                            "astronomical": "0.2159055137",
                            "lunar": "83.9872448293",
                            "kilometers": "32299004.970775819",
                            "miles": "20069671.0458275422"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3843625?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3843625",
                "neo_reference_id": "3843625",
                "name": "(2019 QD3)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3843625",
                "absolute_magnitude_h": 22.481,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.084792019,
                        "estimated_diameter_max": 0.1896007185
                    },
                    "meters": {
                        "estimated_diameter_min": 84.7920190282,
                        "estimated_diameter_max": 189.6007184966
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0526873017,
                        "estimated_diameter_max": 0.1178123881
                    },
                    "feet": {
                        "estimated_diameter_min": 278.1890477085,
                        "estimated_diameter_max": 622.0496212722
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 12:53",
                        "epoch_date_close_approach": 1567687980000,
                        "relative_velocity": {
                            "kilometers_per_second": "6.5279172234",
                            "kilometers_per_hour": "23500.5020042078",
                            "miles_per_hour": "14602.2980370035"
                        },
                        "miss_distance": {
                            "astronomical": "0.2041094838",
                            "lunar": "79.3985891982",
                            "kilometers": "30534344.023279506",
                            "miles": "18973161.5788729428"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3843593?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3843593",
                "neo_reference_id": "3843593",
                "name": "(2019 QE1)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3843593",
                "absolute_magnitude_h": 25.157,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.0247260633,
                        "estimated_diameter_max": 0.0552891583
                    },
                    "meters": {
                        "estimated_diameter_min": 24.7260632932,
                        "estimated_diameter_max": 55.2891583396
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0153640587,
                        "estimated_diameter_max": 0.0343550796
                    },
                    "feet": {
                        "estimated_diameter_min": 81.1222574949,
                        "estimated_diameter_max": 181.3948822468
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 21:39",
                        "epoch_date_close_approach": 1567719540000,
                        "relative_velocity": {
                            "kilometers_per_second": "6.6432674263",
                            "kilometers_per_hour": "23915.7627346558",
                            "miles_per_hour": "14860.3249058754"
                        },
                        "miss_distance": {
                            "astronomical": "0.0337655702",
                            "lunar": "13.1348068078",
                            "kilometers": "5051257.381255474",
                            "miles": "3138705.7929905812"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3843733?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3843733",
                "neo_reference_id": "3843733",
                "name": "(2019 QK7)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3843733",
                "absolute_magnitude_h": 25.483,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.0212791843,
                        "estimated_diameter_max": 0.0475817027
                    },
                    "meters": {
                        "estimated_diameter_min": 21.2791843283,
                        "estimated_diameter_max": 47.5817026638
                    },
                    "miles": {
                        "estimated_diameter_min": 0.013222268,
                        "estimated_diameter_max": 0.0295658902
                    },
                    "feet": {
                        "estimated_diameter_min": 69.8135991117,
                        "estimated_diameter_max": 156.1079533676
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 12:20",
                        "epoch_date_close_approach": 1567686000000,
                        "relative_velocity": {
                            "kilometers_per_second": "4.8976046148",
                            "kilometers_per_hour": "17631.3766132119",
                            "miles_per_hour": "10955.4517628038"
                        },
                        "miss_distance": {
                            "astronomical": "0.1051865994",
                            "lunar": "40.9175871666",
                            "kilometers": "15735691.222783278",
                            "miles": "9777705.1276261164"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3843793?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3843793",
                "neo_reference_id": "3843793",
                "name": "(2019 RM1)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3843793",
                "absolute_magnitude_h": 27.791,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.0073511448,
                        "estimated_diameter_max": 0.0164376595
                    },
                    "meters": {
                        "estimated_diameter_min": 7.3511447866,
                        "estimated_diameter_max": 16.4376594553
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0045677882,
                        "estimated_diameter_max": 0.0102138849
                    },
                    "feet": {
                        "estimated_diameter_min": 24.1179298617,
                        "estimated_diameter_max": 53.9293306473
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 22:06",
                        "epoch_date_close_approach": 1567721160000,
                        "relative_velocity": {
                            "kilometers_per_second": "7.5782385249",
                            "kilometers_per_hour": "27281.6586897236",
                            "miles_per_hour": "16951.7617564008"
                        },
                        "miss_distance": {
                            "astronomical": "0.0209012566",
                            "lunar": "8.1305888174",
                            "kilometers": "3126783.467683442",
                            "miles": "1942893.1536658196"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            },
            {
                "links": {
                    "self": "http://www.neowsapp.com/rest/v1/neo/3843796?api_key=BzB2orclxfvtyCtkKwX3PVr5xgdTQKSjo0JYQeVv"
                },
                "id": "3843796",
                "neo_reference_id": "3843796",
                "name": "(2019 RP1)",
                "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3843796",
                "absolute_magnitude_h": 27.779,
                "estimated_diameter": {
                    "kilometers": {
                        "estimated_diameter_min": 0.0073918812,
                        "estimated_diameter_max": 0.0165287488
                    },
                    "meters": {
                        "estimated_diameter_min": 7.3918811691,
                        "estimated_diameter_max": 16.5287487757
                    },
                    "miles": {
                        "estimated_diameter_min": 0.0045931006,
                        "estimated_diameter_max": 0.0102704852
                    },
                    "feet": {
                        "estimated_diameter_min": 24.2515794148,
                        "estimated_diameter_max": 54.2281801332
                    }
                },
                "is_potentially_hazardous_asteroid": false,
                "close_approach_data": [
                    {
                        "close_approach_date": "2019-09-05",
                        "close_approach_date_full": "2019-Sep-05 22:04",
                        "epoch_date_close_approach": 1567721040000,
                        "relative_velocity": {
                            "kilometers_per_second": "25.875953071",
                            "kilometers_per_hour": "93153.43105547",
                            "miles_per_hour": "57881.9194244391"
                        },
                        "miss_distance": {
                            "astronomical": "0.0002531122",
                            "lunar": "0.0984606458",
                            "kilometers": "37865.045991014",
                            "miles": "23528.2485594332"
                        },
                        "orbiting_body": "Earth"
                    }
                ],
                "is_sentry_object": false
            }
        ]
   }
}

Your model:


import javax.annotation.Generated;
import com.google.gson.annotations.SerializedName;

@Generated("net.hexar.json2pojo")
@SuppressWarnings("unused")
public class Asteroids {

    @SerializedName("near_earth_objects")
    private NearEarthObjects mNearEarthObjects;

    public NearEarthObjects getNearEarthObjects() {
        return mNearEarthObjects;
    }

    public void setNearEarthObjects(NearEarthObjects nearEarthObjects) {
        mNearEarthObjects = nearEarthObjects;
    }
}
// And a lot of subclasses

What you expected:

I have class names like "_2019-09-05" and I want to assign numeric json components to those classes.

What you did:

ObjectMapper objectMapper = new ObjectMapper();
        //  objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        if (response.isSuccessful()) {
            try {
                ex = objectMapper.readValue(data.toString(), Asteroids.class);
            } catch (JsonMappingException e) {
                e.printStackTrace();
            }
        }
        System.out.println(ex.toString());

What you got:

 at [Source: (StringReader); line: 1, column: 16] (through reference chain: com.nurettinabaci.Asteroids["2019-09-05"])
    at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:840)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1179)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1592)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1570)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:294)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173)
    at com.nurettinabaci.Main.main(Main.java:41)
Exception in thread "main" java.lang.NullPointerException
    at com.nurettinabaci.Main.main(Main.java:46)