Skip to content

Nested Objects #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
anti43 opened this issue Jan 30, 2018 · 7 comments
Open

Nested Objects #150

anti43 opened this issue Jan 30, 2018 · 7 comments

Comments

@anti43
Copy link

anti43 commented Jan 30, 2018

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.

@taowen
Copy link
Contributor

taowen commented Feb 5, 2018

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 taowen closed this as completed Feb 5, 2018
@anti43
Copy link
Author

anti43 commented Feb 20, 2018

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"

@taowen taowen reopened this Feb 24, 2018
@paulpolushkin
Copy link

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

@taowen
Copy link
Contributor

taowen commented Feb 25, 2018

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

@anti43 can you provide your class definition?

@anti43
Copy link
Author

anti43 commented May 22, 2018

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?

@sidohin-felix
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants