@Test public void testDeserializeInvalidJson() { try { JsonFormatter.deserialize("{\"name\":"); } catch(UnrecognizedMessageTypeException e) { return; } Assert.fail(); }
public static VehicleMessage deserialize(String data) throws UnrecognizedMessageTypeException { JsonObject root; try { JsonParser parser = new JsonParser(); root = parser.parse(data).getAsJsonObject(); } catch(JsonSyntaxException | IllegalStateException e) { throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } Set<String> fields = new HashSet<>(); for(Map.Entry<String, JsonElement> entry : root.entrySet()) { fields.add(entry.getKey()); } VehicleMessage message; try { if (CanMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, CanMessage.class); } else if (DiagnosticResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, DiagnosticResponse.class); } else if (Command.containsRequiredFields(fields)) { message = sGson.fromJson(root, Command.class); } else if (CommandResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, CommandResponse.class); if (((CommandResponse) message).getCommand() == null) { message = sGson.fromJson(root, CustomCommandResponse.class); } } else if (EventedSimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, EventedSimpleVehicleMessage.class); } else if (SimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, SimpleVehicleMessage.class); } else if (NamedVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, NamedVehicleMessage.class); } else if (MultiFrameResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, MultiFrameResponse.class); } else if (fields.contains(VehicleMessage.EXTRAS_KEY)) { message = sGson.fromJson(root, VehicleMessage.class); } else { throw new UnrecognizedMessageTypeException( "Unrecognized combination of fields: " + fields); } }catch (NumberFormatException e){ throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } return message; }
JsonFormatter { public static VehicleMessage deserialize(String data) throws UnrecognizedMessageTypeException { JsonObject root; try { JsonParser parser = new JsonParser(); root = parser.parse(data).getAsJsonObject(); } catch(JsonSyntaxException | IllegalStateException e) { throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } Set<String> fields = new HashSet<>(); for(Map.Entry<String, JsonElement> entry : root.entrySet()) { fields.add(entry.getKey()); } VehicleMessage message; try { if (CanMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, CanMessage.class); } else if (DiagnosticResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, DiagnosticResponse.class); } else if (Command.containsRequiredFields(fields)) { message = sGson.fromJson(root, Command.class); } else if (CommandResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, CommandResponse.class); if (((CommandResponse) message).getCommand() == null) { message = sGson.fromJson(root, CustomCommandResponse.class); } } else if (EventedSimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, EventedSimpleVehicleMessage.class); } else if (SimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, SimpleVehicleMessage.class); } else if (NamedVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, NamedVehicleMessage.class); } else if (MultiFrameResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, MultiFrameResponse.class); } else if (fields.contains(VehicleMessage.EXTRAS_KEY)) { message = sGson.fromJson(root, VehicleMessage.class); } else { throw new UnrecognizedMessageTypeException( "Unrecognized combination of fields: " + fields); } }catch (NumberFormatException e){ throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } return message; } }
JsonFormatter { public static VehicleMessage deserialize(String data) throws UnrecognizedMessageTypeException { JsonObject root; try { JsonParser parser = new JsonParser(); root = parser.parse(data).getAsJsonObject(); } catch(JsonSyntaxException | IllegalStateException e) { throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } Set<String> fields = new HashSet<>(); for(Map.Entry<String, JsonElement> entry : root.entrySet()) { fields.add(entry.getKey()); } VehicleMessage message; try { if (CanMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, CanMessage.class); } else if (DiagnosticResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, DiagnosticResponse.class); } else if (Command.containsRequiredFields(fields)) { message = sGson.fromJson(root, Command.class); } else if (CommandResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, CommandResponse.class); if (((CommandResponse) message).getCommand() == null) { message = sGson.fromJson(root, CustomCommandResponse.class); } } else if (EventedSimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, EventedSimpleVehicleMessage.class); } else if (SimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, SimpleVehicleMessage.class); } else if (NamedVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, NamedVehicleMessage.class); } else if (MultiFrameResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, MultiFrameResponse.class); } else if (fields.contains(VehicleMessage.EXTRAS_KEY)) { message = sGson.fromJson(root, VehicleMessage.class); } else { throw new UnrecognizedMessageTypeException( "Unrecognized combination of fields: " + fields); } }catch (NumberFormatException e){ throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } return message; }  }
JsonFormatter { public static VehicleMessage deserialize(String data) throws UnrecognizedMessageTypeException { JsonObject root; try { JsonParser parser = new JsonParser(); root = parser.parse(data).getAsJsonObject(); } catch(JsonSyntaxException | IllegalStateException e) { throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } Set<String> fields = new HashSet<>(); for(Map.Entry<String, JsonElement> entry : root.entrySet()) { fields.add(entry.getKey()); } VehicleMessage message; try { if (CanMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, CanMessage.class); } else if (DiagnosticResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, DiagnosticResponse.class); } else if (Command.containsRequiredFields(fields)) { message = sGson.fromJson(root, Command.class); } else if (CommandResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, CommandResponse.class); if (((CommandResponse) message).getCommand() == null) { message = sGson.fromJson(root, CustomCommandResponse.class); } } else if (EventedSimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, EventedSimpleVehicleMessage.class); } else if (SimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, SimpleVehicleMessage.class); } else if (NamedVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, NamedVehicleMessage.class); } else if (MultiFrameResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, MultiFrameResponse.class); } else if (fields.contains(VehicleMessage.EXTRAS_KEY)) { message = sGson.fromJson(root, VehicleMessage.class); } else { throw new UnrecognizedMessageTypeException( "Unrecognized combination of fields: " + fields); } }catch (NumberFormatException e){ throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } return message; }  static String serialize(VehicleMessage message); static String serialize(Collection<VehicleMessage> messages); static VehicleMessage deserialize(String data); }
JsonFormatter { public static VehicleMessage deserialize(String data) throws UnrecognizedMessageTypeException { JsonObject root; try { JsonParser parser = new JsonParser(); root = parser.parse(data).getAsJsonObject(); } catch(JsonSyntaxException | IllegalStateException e) { throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } Set<String> fields = new HashSet<>(); for(Map.Entry<String, JsonElement> entry : root.entrySet()) { fields.add(entry.getKey()); } VehicleMessage message; try { if (CanMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, CanMessage.class); } else if (DiagnosticResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, DiagnosticResponse.class); } else if (Command.containsRequiredFields(fields)) { message = sGson.fromJson(root, Command.class); } else if (CommandResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, CommandResponse.class); if (((CommandResponse) message).getCommand() == null) { message = sGson.fromJson(root, CustomCommandResponse.class); } } else if (EventedSimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, EventedSimpleVehicleMessage.class); } else if (SimpleVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, SimpleVehicleMessage.class); } else if (NamedVehicleMessage.containsRequiredFields(fields)) { message = sGson.fromJson(root, NamedVehicleMessage.class); } else if (MultiFrameResponse.containsRequiredFields(fields)) { message = sGson.fromJson(root, MultiFrameResponse.class); } else if (fields.contains(VehicleMessage.EXTRAS_KEY)) { message = sGson.fromJson(root, VehicleMessage.class); } else { throw new UnrecognizedMessageTypeException( "Unrecognized combination of fields: " + fields); } }catch (NumberFormatException e){ throw new UnrecognizedMessageTypeException( "Unable to parse JSON from \"" + data + "\": " + e); } return message; }  static String serialize(VehicleMessage message); static String serialize(Collection<VehicleMessage> messages); static VehicleMessage deserialize(String data);  }