java icon indicating copy to clipboard operation
java copied to clipboard

Nested Objects

Open anti43 opened this issue 7 years ago • 7 comments

Hi, Is it possible to deserialize nested Objects? Given classes

class A{
    String name
}
class B {
   int prop
   A someA
}

and json

[
   {
     "someA": {
          "name": "test1"
      },
    "prop": 2323
   },
   {
     "someA": {
          "name": "test2"
      },
    "prop": 2324
   }
]

is this supposed to work with

Joniterator.parse(json).read(B[].class)

?

I am getting error messages

com.jsoniter.spi.JsonException: readString: expect string or null, but 0, head: 444, peek: cInKmh": Of course the class and json struct for real is much more complex, but the idea is the same. What might be wrong here? With gson, it just worked with same json code.

anti43 avatar Jan 30 '18 10:01 anti43

public static class A{
        String name;
    }
    public static class B {
        int prop;
        A someA;
    }
    public void test_abc1() throws IOException {
        JsonIterator.parse("[\n" +
                "   {\n" +
                "     \"someA\": {\n" +
                "          \"name\": \"test1\"\n" +
                "      },\n" +
                "    \"prop\": 2323\n" +
                "   },\n" +
                "   {\n" +
                "     \"someA\": {\n" +
                "          \"name\": \"test2\"\n" +
                "      },\n" +
                "    \"prop\": 2324\n" +
                "   }\n" +
                "]").read(B[].class);
    }

It is not throwing exception on my machine. Please check the input codec. Only UTF-8 is supported.

taowen avatar Feb 05 '18 15:02 taowen

This is the json failing:

{"country": "FRA","speedCategory": "4","typeName": "ExpressHighway","dist": 0,"lon": 5.757277,"isBridge": "N","latMatched": 49.53308,"roadName": "N52","possibleSpeed": 90,"laneCategory": "2","signs": [],"routeLinkSeqNum": 2,"lonMatched": 5.75726,"pois": [],"lat": 49.53308,"bearing": 0,"index": 4,"speedLimit": 90,"loc_id": 904,"isTunnel": "N","routeTypesMask": "4","linkId": 1175216557,"urban": "N","traficInKmh": 0,"capitalsOnLink": [],"lowMobility": "3","isLongHaul": "N","fc": 2,"clazz": 11}

so probably its not about nested objects, but what is it? Its UTF8 and and jsonlint reports it as OK

expect string or null, but 0, head: 4944, peek: cInKmh"

anti43 avatar Feb 20 '18 09:02 anti43

I get the same error when "traficInKmh" property has String type in Java class but in JSON it has not quotes as in the example above

paulpolushkin avatar Feb 24 '18 21:02 paulpolushkin

@paulpolushkin if type is string, the JSON should have quotes.

@anti43 can you provide your class definition?

taowen avatar Feb 25 '18 01:02 taowen

Hi, @paulpolushkin is right, the property is declared as String but its an int for real. Maybe the error message should be a little bit more clear.

e.g expect string TYPE or null, but found INT property, head: 4944, peek: cInKmh"

:) Dont you think String type should work for anything?

anti43 avatar May 22 '18 06:05 anti43

Hi guys,

I am also having a similar issue, however mine occurs specifically when a HashMap is involved - was wondering if it is somehow related.

We feed the following string (which is actually produced by Jsoniter):

{"enabled":false,"configId":"1878267263773732","minRequiredEmploymentPeriodMetric":"MONTH","minRequiredEmploymentPeriod":1,"allowedWithdrawalRatio":70,"payrollCutoffMetric":"DAY","payrollCutoff":3,"payrollConfiguration":{"day2":15,"payrollMode":"DAY_OF_MONTH","day1":1,"payrollFrequencyMetric":null,"payrollFrequency":0,"payrollDay":null},"employeeId":null,"locationId":null,"companyId":"126262","configurationScope":"COMPANY","lockedProperties":["minRequiredEmploymentPeriodMetric"],"gatewayConfigurations":{"ASI":{"gatewayName":"ASI","paymentDestinationConfigurations":{"DEBIT_CARD":{"enabled":true,"maxWithdrawalCount":1}}}}}

into the iterator and end up with the following error:

com.jsoniter.spi.JsonException: readSlice: expect " for string, head: 574, peek: tions":{"D, buf: {"enabled":false,"configId":"1878267263773732","minRequiredEmploymentPeriodMetric":"MONTH","minRequiredEmploymentPeriod":1,"allowedWithdrawalRatio":70,"payrollCutoffMetric":"DAY","payrollCutoff":3,"payrollConfiguration":{"day2":15,"payrollMode":"DAY_OF_MONTH","day1":1,"payrollFrequencyMetric":null,"payrollFrequency":0,"payrollDay":null},"employeeId":null,"locationId":null,"companyId":"126262","configurationScope":"COMPANY","lockedProperties":["minRequiredEmploymentPeriodMetric"],"gatewayConfigurations":{"ASI":{"gatewayName":"ASI","paymentDestinationConfigurations":{"DEBIT_CARD":{"enabled":true,"maxWithdrawalCount":1}}}}}

Any idea why this could happen?

sidohin-felix avatar Dec 17 '19 21:12 sidohin-felix

Actually to give you some background - the problem disappears when we set the HashMap key to be a String - the problem occurs when the key is an enum.

sidohin-felix avatar Dec 17 '19 22:12 sidohin-felix