简体   繁体   中英

GSON and deserialization error

Good day!

I bet over a mistake for several days, thoughts ran. Have an ArrayList of objects being serialized GSON and stored in MySQL in the TEXT.

This is function to deserialization:

private void listenBroadcasts() {

    try {
        while (!shutdownThreads)
        {
            try
            {
                ResultSet rs = sql.select("SELECT * FROM messages WHERE time > (now() - INTERVAL 2 SECOND)");

                    while (rs.next()) {

                        String subject = rs.getString("subject");
                        String jsonString = rs.getString("json");
                        String className = rs.getString("class");
                        int id = rs.getInt("id");

                        if (jsonSubjects.contains(subject)
                                && !StringUtils.isEmpty(className)
                                && !StringUtils.isEmpty(jsonString)
                                && id != myLastID) {

                            LOGGER.info("JSON: "+jsonString);

                            final Class clazz = Class.forName(className);
                            Object object = gson.fromJson(jsonString, clazz);
                            JsonEnvelope envelope = new JsonEnvelope(rs.getString("sender"), null, subject, object);

                            LOGGER.debug("Received message: "
                                    + " sender: " + envelope.getSenderInstance()
                                    + " receiver: " + envelope.getReceiverInstance()
                                    + " to subject: "
                                    + envelope.getSubject() + " (" + envelope.getClass().getSimpleName() + ")");
                            myLastID = id;
                            jsonReceiveQueue.put(envelope);
                        }
                    }

                    rs.close();

                Thread.sleep(1000L);

            } catch (final SQLException e) {
                LOGGER.debug("Error receiving JSON message ", e);
            } catch (final ClassNotFoundException e) {
                LOGGER.error("Error deserializing JSON message ", e);
            } catch (final InterruptedException e)
            {
                //LOGGER.error("Interrupt erro in JSOM message ", e);
            }

        }
    } catch (Exception e) {
        LOGGER.error("Error JsonMessaging: "+e.toString());
        e.printStackTrace();
    }
}

When sending a single object, all proceeds normally - the object is serialized and desializuetsya normal. Once the array is passed - it normally serialized, while desearilizatsii falls error.

INFO (messaging.JsonMessaging) - JSON:

{"devices":["{
  "name": "not set",
  "node": 1,
  "zone": 0,
  "type": "Portable Remote Controller",
  "internalType": "controller",
  "manufName": "undefined",
  "productName": "",
  "uuid": "413e3621-f4eb-4469-b986-78699455541c",
  "status": "Listening",
  "source": "zwave",
  "internalName": "zwave/controller/1",
  "values": {
    "Basic": "0"
  }
}","{
  "name": "not set",
  "node": 4,
  "zone": 0,
  "type": "Routing Binary Sensor",
  "internalType": "binarysensor",
  "manufName": "undefined",
  "productName": "",
  "uuid": "0dd6743c-6250-4cda-ab5c-871844090642",
  "status": "Sleeping",
  "source": "zwave",
  "internalName": "zwave/binarysensor/4",
  "values": {
    "Sensor": "false",
    "Wake-up Interval": "3600"
  }
}","{
  "name": "not set",
  "node": 3,
  "zone": 0,
  "type": "Multilevel Power Switch",
  "internalType": "dimmer",
  "manufName": "FIBARO System",
  "productName": "FGD211 Universal Dimmer 500W",
  "uuid": "c00228e5-45cc-4ac4-8371-9ac179857926",
  "status": "Listening",
  "source": "zwave",
  "internalName": "zwave/dimmer/3",
  "values": {
    "Timeout": "0",
    "1. Enable/Disable ALL ON/OFF": "null",
    "20. ADVANCED Impulse length": "110",
    "Frame Count": "0",
    "17. 3-way switch": "null",
    "8. Dimming step at automatic control": "1",
    "Report": "null",
    "Protocol Version": "3.34",
    "Application Version": "1.06",
    "Test": "null",
    "13. Minimum dimmer level control": "2",
    "10. Time of AUTOMATIC moving between the extreme dimming values": "1",
    "Test Powerlevel": "null",
    "Start Level": "0",
    "Bright": "null",
    "Library Version": "3",
    "12. Maximum dimmer level control": "99",
    "16. Saving state before power faillure": "null",
    "Test Node": "0",
    "39. ALARM FLASHING alarm time": "600",
    "14. Inputs Button/Switch configuration": "null",
    "Dim": "null",
    "18. Synchronizing light level for associated devices": "null",
    "7. Control key #2 behaviour": "null",
    "11. Dimming step at manual control": "1",
    "15. Double click option": "null",
    "30. Alarm": "null",
    "9. Time of MANUALLY moving between the extreme dimming values": "5",
    "Powerlevel": "null",
    "19. Change [On-Off] bi-stable keys": "null",
    "Test Status": "null",
    "Ignore Start Level": "true",
    "Level": "99",
    "Set Powerlevel": "null",
    "Switch All": "null",
    "6. Separation of association sending (key 1)": "null",
    "Acked Frames": "64"
  }
}","{
  "name": "not set",
  "node": 2,
  "zone": 0,
  "type": "Routing Alarm Sensor",
  "internalType": "alarmsensor",
  "manufName": "undefined",
  "productName": "",
  "uuid": "1588357d-fdf0-4813-b447-10158a72be80",
  "status": "Sleeping",
  "source": "zwave",
  "internalName": "zwave/alarmsensor/2",
  "values": {
    "Application Version": "Unknown",
    "Alarm Type": "0",
    "Battery Level": "100",
    "Wake-up Interval": "3600",
    "Protocol Version": "Unknown",
    "Alarm Level": "0",
    "Library Version": "Unknown"
  }
}"]}

26 Nov 2013 12:15:04

 ERROR (messaging.JsonMessaging) - Error JsonMessaging: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 5
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 5
        at com.google.gson.Gson.fromJson(Gson.java:818)
        at com.google.gson.Gson.fromJson(Gson.java:768)
        at com.google.gson.Gson.fromJson(Gson.java:717)
        at com.google.gson.Gson.fromJson(Gson.java:689)
        at ru.iris.common.messaging.JsonMessaging.listenBroadcasts(JsonMessaging.java:164)
        at ru.iris.common.messaging.JsonMessaging.access$000(JsonMessaging.java:37)
        at ru.iris.common.messaging.JsonMessaging$1.run(JsonMessaging.java:63)
        at java.lang.Thread.run(Thread.java:724)
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated array at line 2 column 5
        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
        at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:465)
        at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
        at com.google.gson.Gson.fromJson(Gson.java:803)
        ... 7 more

The gson error you have is correct as the JSON you have supplied is invalid.

Use a tool like http://jsonlint.com/ to check your JSON.

I have run your supplied JSON through formatter and corrected the problem:

{
"devices": [
    {
        "name": "not set",
        "node": 1,
        "zone": 0,
        "type": "Portable Remote Controller",
        "internalType": "controller",
        "manufName": "undefined",
        "productName": "",
        "uuid": "413e3621-f4eb-4469-b986-78699455541c",
        "status": "Listening",
        "source": "zwave",
        "internalName": "zwave/controller/1",
        "values": {
            "Basic": "0"
        }
    },
    {
        "name": "not set",
        "node": 4,
        "zone": 0,
        "type": "Routing Binary Sensor",
        "internalType": "binarysensor",
        "manufName": "undefined",
        "productName": "",
        "uuid": "0dd6743c-6250-4cda-ab5c-871844090642",
        "status": "Sleeping",
        "source": "zwave",
        "internalName": "zwave/binarysensor/4",
        "values": {
            "Sensor": "false",
            "Wake-up Interval": "3600"
        }
    },
    {
        "name": "not set",
        "node": 3,
        "zone": 0,
        "type": "Multilevel Power Switch",
        "internalType": "dimmer",
        "manufName": "FIBARO System",
        "productName": "FGD211 Universal Dimmer 500W",
        "uuid": "c00228e5-45cc-4ac4-8371-9ac179857926",
        "status": "Listening",
        "source": "zwave",
        "internalName": "zwave/dimmer/3",
        "values": {
            "Timeout": "0",
            "1. Enable/Disable ALL ON/OFF": "null",
            "20. ADVANCED Impulse length": "110",
            "Frame Count": "0",
            "17. 3-way switch": "null",
            "8. Dimming step at automatic control": "1",
            "Report": "null",
            "Protocol Version": "3.34",
            "Application Version": "1.06",
            "Test": "null",
            "13. Minimum dimmer level control": "2",
            "10. Time of AUTOMATIC moving between the extreme dimming values": "1",
            "Test Powerlevel": "null",
            "Start Level": "0",
            "Bright": "null",
            "Library Version": "3",
            "12. Maximum dimmer level control": "99",
            "16. Saving state before power faillure": "null",
            "Test Node": "0",
            "39. ALARM FLASHING alarm time": "600",
            "14. Inputs Button/Switch configuration": "null",
            "Dim": "null",
            "18. Synchronizing light level for associated devices": "null",
            "7. Control key #2 behaviour": "null",
            "11. Dimming step at manual control": "1",
            "15. Double click option": "null",
            "30. Alarm": "null",
            "9. Time of MANUALLY moving between the extreme dimming values": "5",
            "Powerlevel": "null",
            "19. Change [On-Off] bi-stable keys": "null",
            "Test Status": "null",
            "Ignore Start Level": "true",
            "Level": "99",
            "Set Powerlevel": "null",
            "Switch All": "null",
            "6. Separation of association sending (key 1)": "null",
            "Acked Frames": "64"
        }
    },
    {
        "name": "not set",
        "node": 2,
        "zone": 0,
        "type": "Routing Alarm Sensor",
        "internalType": "alarmsensor",
        "manufName": "undefined",
        "productName": "",
        "uuid": "1588357d-fdf0-4813-b447-10158a72be80",
        "status": "Sleeping",
        "source": "zwave",
        "internalName": "zwave/alarmsensor/2",
        "values": {
            "Application Version": "Unknown",
            "Alarm Type": "0",
            "Battery Level": "100",
            "Wake-up Interval": "3600",
            "Protocol Version": "Unknown",
            "Alarm Level": "0",
            "Library Version": "Unknown"
        }
    }
]  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM