[Java] Jackson JSON 'Unrecognized token ***

[Java] Jackson JSON 'Unrecognized token ***

Problem

  • I receive JSON data from Django REST Server
  • However, it caused ‘Unrecognized token’ error
  • In my case, this error occured by ‘True’ and ‘None’
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
2019-09-06 17:20:57.019 28853-28945/*** E/dorbae: Failed to get DB data.
    com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'True': was expecting ('true', 'false' or 'null')
     at [Source: (byte[])"{'name': 'WFS', 'columns': ['time', 'current'], 'values': [['2019-08-24T15:20:31+09:00', 7], ['2019-08-24T11:28:41+09:00', 6], ['2019-08-24T06:29:02+09:00', 4], ['2019-08-24T05:50:00+09:00', 4], ['2019-08-24T02:15:17+09:00', 2], ['2019-08-24T02:15:12+09:00', 2], ['2019-08-23T18:05:16+09:00', 2], ['2019-08-23T17:24:44+09:00', 4], ['2019-08-23T17:22:04+09:00', 2], ['2019-08-23T17:21:59+09:00', 2], ['2019-08-23T06:36:24+09:00', 4], ['2019-08-23T03:45:43+09:00', 2], ['2019-08-23T03:45:38+09:00', 2],"[truncated 1953313 bytes]; line: 1, column: 1953814]
        at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
        at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3532)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleUnexpectedValue(UTF8StreamJsonParser.java:2627)
        at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:1053)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:295)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3091)
        at 
...


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
2019-09-06 17:30:03.244 29666-29758/*** E/dorbae: com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token 'None': was expecting 'null', 'true', 'false' or NaN
        at [Source: (byte[])"{'name': 'qbsiotseries', 'columns': ['time', 'TEMPERATURE', 'HUMIDITY', 'CO2PPM', 'ILLUMINANCE', 'VALUE00', 'VALUE01', 'VALUE02', 'VALUE03'], 'values': [['2019-08-23T08:00:00Z', None, None, None, None, None, None, None, None], ['2019-08-23T09:00:00Z', None, None, None, None, None, None, None, None], ['2019-08-23T10:00:00Z', None, None, None, None, None, None, None, None], ['2019-08-23T11:00:00Z', None, None, None, None, None, None, None, None], ['2019-08-23T12:00:00Z', None, None, None, None, No"[truncated 25509 bytes]; line: 1, column: 179] (through reference chain: ***.InfluxDBResultSet["values"]->java.lang.Object[][0])
        at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:206)
        at com.fasterxml.jackson.databind.deser.std.ObjectArrayDeserializer.deserialize(ObjectArrayDeserializer.java:21)
        at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3091)
        at 
 
 ...


Cause

  • My architecture

screenshot001


  • Jaskcon libarary was not able to recognize the tokens such as ‘True’, ‘None’ and my django server didn’t nomalize json data


Solution

1. Nomalize JSON data in Django server

  • json.dumps method nomalizes JSON data followed by ECMA-404
  • True -> true & None -> null
  • Add this code
import json

...

json.dumps(jsonObject)

...


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 client = InfluxDBClient(host, port, user, password, db_name)
    result = client.query(query)
    _value = ''

    _series = result.raw.get('series')

    if _series is not None:
        # None -> Null & Object -> Object[]
        _value = json.dumps(_series)
        return HttpResponse(_value, status=200)
    # return HTTP Response
    return HttpResponse(_value, status=404)


2. Skip null data in Android

  • Add this code
...

ObjectMapper mapper = new ObjectMapper();

...

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

...

1
2
3
4
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 싱글 쿼테이션 허용
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
InfluxDBResultSet[] influxDBResultSetArray = mapper.readValue(data, InfluxDBResultSet[].class);



References

댓글남기기

-->