2013-11-26 2 views
0

안녕하세요!GSON 및 비 직렬화 오류

며칠 동안 실수로 내기를했지만 생각은 뛰었습니다. Object의 ArrayList를 직렬화 된 GSON으로 만들고 TEXT에 MySQL에 저장합니다.

이 직렬화에 기능입니다 :

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(); 
    } 
} 

단일 객체를 전송, 일반적으로 모든 진행 - 객체 직렬화 및 desializuetsya 정상입니다. 배열이 전달되면 일반적으로 직렬화되지만 desearilizatsii는 오류가 발생합니다.

정보 (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" 
    } 
}"]} 

11월 26일 2013 12시 15분 4초

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 
+0

귀하의 질문에 오류 메시지와 JSON 포맷을 고려하세요. 당신은 아마 또한 당신의 제목을 정정하고자합니다. – Seanny123

+0

JSON 메시지를이 [formatter] (http://jsonformatter.curiousconcept.com/)에 넣으면 JSON 오류가 무엇인지 알려주고 진행 상황을 추적하는 데 도움이 될 수 있습니다. 나는 당신의 JSON 메시지에 실수를 많이 볼 수 있습니다 { "장치": [ "{"이름 ":" ", "노드 "로 설정하지 : 1, (...)를 당신이 볼 수 – PerGon

+0

앞으로는 문제와 관련된 최소한의 코드를 요약하십시오. 데이터베이스에 대한 쿼리는 아닙니다. – migu

답변

2

당신이 가지고있는 GSON 오류는 사용자가 제공 한 JSON으로 올바른 무효입니다 .

http://jsonlint.com/과 같은 도구를 사용하여 JSON을 확인하십시오.

나는 포맷터를 통해 제공된 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" 
     } 
    } 
] }