List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:gov.pnnl.goss.gridappsd.configuration.GLDSimulationOutputConfigurationHandler.java
License:Open Source License
void parseMeasurement(Map<String, JsonArray> measurements, JsonObject measurement) throws JsonParseException { String objectName;/*from w w w . jav a 2s . c o m*/ String propertyName; String measurementType; String phases; String conductingEquipmentType; String conductingEquipmentName; String connectivityNode; if (!measurement.has("measurementType") || !measurement.has("phases") || !measurement.has("ConductingEquipment_type") || !measurement.has("ConductingEquipment_name") || !measurement.has("ConnectivityNode")) { throw new JsonParseException( "CimMeasurementsToGldPubs::parseMeasurement: The JsonObject measurements must have the following keys: measurementType, phases, ConductingEquipment_type,ConductingEquipment_name, and ConnectivityNode."); } measurementType = measurement.get("measurementType").getAsString(); phases = measurement.get("phases").getAsString(); if (phases.equals("s1")) { phases = "1"; } else if (phases.equals("s2")) { phases = "2"; } conductingEquipmentType = measurement.get("name").getAsString(); conductingEquipmentName = measurement.get("ConductingEquipment_name").getAsString(); connectivityNode = measurement.get("ConnectivityNode").getAsString(); if (conductingEquipmentType.contains("LinearShuntCompensator")) { if (measurementType.equals("VA")) { objectName = "cap_" + conductingEquipmentName; propertyName = "shunt_" + phases; } else if (measurementType.equals("Pos")) { objectName = "cap_" + conductingEquipmentName; propertyName = "switch" + phases; } else if (measurementType.equals("PNV")) { objectName = "cap_" + conductingEquipmentName; propertyName = "voltage_" + phases; } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of measurementType is not a valid type.\nValid types for LinearShuntCompensators are VA, Pos, and PNV.\nmeasurementType = %s.", measurementType)); } } else if (conductingEquipmentType.contains("PowerTransformer")) { if (measurementType.equals("VA")) { objectName = "xf_" + conductingEquipmentName; propertyName = "power_in_" + phases; } else if (measurementType.equals("PNV")) { objectName = connectivityNode; propertyName = "voltage_" + phases; } else if (measurementType.equals("A")) { objectName = "xf_" + conductingEquipmentName; propertyName = "current_in_" + phases; } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of measurementType is not a valid type.\nValid types for PowerTransformers are VA, PNV, and A.\nmeasurementType = %s.", measurementType)); } } else if (conductingEquipmentType.contains("RatioTapChanger")) { if (measurementType.equals("VA")) { objectName = "reg_" + conductingEquipmentName; propertyName = "power_in_" + phases; } else if (measurementType.equals("PNV")) { objectName = connectivityNode; propertyName = "voltage_" + phases; } else if (measurementType.equals("A")) { objectName = "reg_" + conductingEquipmentName; propertyName = "current_in_" + phases; } else if (measurementType.equals("Pos")) { objectName = "reg_" + conductingEquipmentName; propertyName = "tap_" + phases; } else if (measurementType.equals("PNV")) { objectName = connectivityNode; propertyName = "voltage_" + phases; } else if (measurementType.equals("A")) { objectName = "tx_" + conductingEquipmentName; propertyName = "current_out_" + phases; } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of measurementType is not a valid type.\nValid types for RatioTapChanger are VA, PNV, A, and Pos.\nmeasurementType = %s.", measurementType)); } } else if (conductingEquipmentType.contains("ACLineSegment")) { String prefix = ""; if (phases.equals("1") || phases.equals("2")) { prefix = "tpx_"; } else { prefix = "line_"; } if (measurementType.equals("VA")) { objectName = prefix + conductingEquipmentName; propertyName = "power_in_" + phases; } else if (measurementType.equals("PNV")) { objectName = connectivityNode; propertyName = "voltage_" + phases; } else if (measurementType.equals("A")) { objectName = prefix + conductingEquipmentName; propertyName = "current_in_" + phases; } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of measurementType is not a valid type.\nValid types for ACLineSegments are VA, A, and PNV.\nmeasurementType = %s.", measurementType)); } } else if (conductingEquipmentType.contains("LoadBreakSwitch")) { if (measurementType.equals("VA")) { objectName = "swt_" + conductingEquipmentName; propertyName = "power_in_" + phases; } else if (measurementType.equals("PNV")) { objectName = connectivityNode; propertyName = "voltage_" + phases; } else if (measurementType.equals("A")) { objectName = "swt_" + conductingEquipmentName; propertyName = "current_in_" + phases; } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of measurementType is not a valid type.\nValid types for LoadBreakSwitch are VA, A, and PNV.\nmeasurementType = %s.", measurementType)); } } else if (conductingEquipmentType.contains("EnergyConsumer")) { if (measurementType.equals("VA")) { objectName = connectivityNode; propertyName = "measured_power_" + phases; } else if (measurementType.equals("PNV")) { objectName = connectivityNode; propertyName = "voltage_" + phases; } else if (measurementType.equals("A")) { objectName = connectivityNode; propertyName = "measured_current_" + phases; } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of measurementType is not a valid type.\nValid types for EnergyConsumer are VA, A, and PNV.\nmeasurementType = %s.", measurementType)); } } else if (conductingEquipmentType.contains("PowerElectronicsConnection")) { if (measurementType.equals("VA")) { objectName = connectivityNode; propertyName = "measured_power_" + phases; } else if (measurementType.equals("PNV")) { objectName = connectivityNode; propertyName = "voltage_" + phases; } else if (measurementType.equals("A")) { objectName = connectivityNode; propertyName = "measured_current_" + phases; } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of measurementType is not a valid type.\nValid types for PowerElectronicsConnection are VA, A, and PNV.\nmeasurementType = %s.", measurementType)); } } else { throw new JsonParseException(String.format( "CimMeasurementsToGldPubs::parseMeasurement: The value of ConductingEquipment_type is not a recognized object type.\nValid types are ACLineSegment, LinearShuntCompesator, RatioTapChanger, LoadBreakSwitch, EnergyConsumer, PowerElectronicsConnection, and PowerTransformer.\nConductingEquipment_type = %s.", conductingEquipmentType)); } if (measurements.containsKey(objectName)) { measurements.get(objectName).add(new JsonPrimitive(propertyName)); } else { JsonArray newMeasurements = new JsonArray(); newMeasurements.add(new JsonPrimitive(propertyName)); measurements.put(objectName, newMeasurements); } }
From source file:gson.util.MessageBodyDeserializer.java
@Override public MessageBody deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext jdc) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); MessageBody message = null;/*from w ww .j a v a 2 s.co m*/ if (jsonObject.get("text_message") != null) { message = new StringMessage(); ((StringMessage) message).setTextMessage(jsonObject.get("text_message").getAsString()); } else if (jsonObject.get("date_from") != null) { message = new GetMessage(); String data = jsonObject.get("date_from").getAsString(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); formatter.setTimeZone(TimeZone.getTimeZone("Europe/Warsaw")); try { Date dataFrom = new Date(formatter.parse(data).getTime()); ((GetMessage) message).setDateFrom(dataFrom); } catch (ParseException ex) { Logger.getLogger(MessageBodyDeserializer.class.getName()).log(Level.SEVERE, null, ex); } } else if (jsonObject.get("healthInfo") != null) { List<Wynik> healthInfo = new ArrayList<Wynik>(); message = new HealthInfoMessage(); JsonArray healthInfoArray = jsonObject.get("healthInfo").getAsJsonArray(); for (JsonElement js : healthInfoArray) { Wynik wynik = jdc.deserialize(js, Wynik.class); healthInfo.add(wynik); } ((HealthInfoMessage) message).setHealthInfo(healthInfo); } else if (jsonObject.get("patientsInfo") != null) { List<Pacjent> patientsInfo = new ArrayList<Pacjent>(); message = new DocHealthInfoMessage(); JsonArray patientsInfoArray = jsonObject.get("patientsInfo").getAsJsonArray(); for (JsonElement js : patientsInfoArray) { Pacjent pacjent = jdc.deserialize(js, Pacjent.class); patientsInfo.add(pacjent); } ((DocHealthInfoMessage) message).setPatientsInfo(patientsInfo); } else throw new JsonParseException("Not supported json!"); if (message != null) message.setKey(jsonObject.get("key").getAsString()); return message; }
From source file:hd3gtv.mydmam.metadata.container.ContainerOperations.java
License:Open Source License
public static JsonObject getJsonObject(JsonElement json, boolean can_null) throws JsonParseException { if (json.isJsonNull()) { if (can_null) { return null; } else {//from ww w . j av a 2s.co m throw new JsonParseException("Json element is null"); } } if (json.isJsonObject() == false) { throw new JsonParseException("Json element is not an object: " + json.toString()); } return (JsonObject) json.getAsJsonObject(); }
From source file:hdm.stuttgart.esell.router.GeneralObjectDeserializer.java
License:Apache License
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; } else if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { return primitive.getAsString(); } else if (primitive.isNumber()) { return primitive.getAsNumber(); } else if (primitive.isBoolean()) { return primitive.getAsBoolean(); }/*from w w w .ja v a 2 s .c o m*/ } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); Object[] result = new Object[array.size()]; int i = 0; for (JsonElement element : array) { result[i] = deserialize(element, null, context); ++i; } return result; } else if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); Map<String, Object> result = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { Object value = deserialize(entry.getValue(), null, context); result.put(entry.getKey(), value); } return result; } else { throw new JsonParseException("Unknown JSON type for JsonElement " + json.toString()); } return null; }
From source file:io.datakernel.serializer.GsonSubclassesAdapter.java
License:Apache License
@SuppressWarnings("unchecked") @Override/*ww w.j av a2s. c om*/ public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { if (!((JsonPrimitive) json).isString()) { throw new JsonParseException("Inner class name is expected"); } String className = json.getAsString(); InstanceCreator<T> creator = classCreators.get(className); if (creator != null) { return creator.createInstance(typeOfT); } try { Class<?> aClass = classTags.get(className); if (aClass == null) { aClass = Class.forName(className); } return (T) newInstance(aClass); } catch (InvocationTargetException | ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException e) { throw new JsonParseException(e); } } JsonObject object = json.getAsJsonObject(); String subclassName = object.get(subclassField).getAsString(); return context.deserialize(json, subclassNames.get(subclassName)); }
From source file:io.hawkcd.agent.utilities.deserializers.MaterialDefinitionAdapter.java
License:Apache License
@Override public MaterialDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.get("type") == null) { throw new JsonParseException("Field type is null!"); }//from w w w . j a v a 2 s . c o m String typeOfMaterial = jsonObject.get("type").getAsString(); Type taskClass = this.materialTypeMap.get(typeOfMaterial); if (taskClass == null) { throw new JsonParseException("Invalid Material Definition type!"); } MaterialDefinition result = this.jsonConverter.fromJson(json, taskClass); return result; }
From source file:io.hawkcd.utilities.deserializers.ConversionObjectDeserializer.java
License:Apache License
@Override public ConversionObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); for (String fieldName : this.requiredFields) { if (jsonObject.get(fieldName) == null) { throw new JsonParseException("Required field not found"); }/*from w w w . j ava2s . com*/ } String packageName = jsonObject.get("packageName").getAsString(); String object = jsonObject.get("object").toString(); if ((packageName == null) || packageName.trim().isEmpty() || (object == null)) { return null; } ConversionObject result = new ConversionObject(); result.setPackageName(packageName); result.setObject(object); return result; }
From source file:io.hawkcd.utilities.deserializers.TaskDefinitionAdapter.java
License:Apache License
@Override public TaskDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); if (jsonObject.get("type") == null) { throw new JsonParseException("Field type is null!"); }/*from w w w. j a va 2 s .co m*/ String typeOfTask = jsonObject.get("type").getAsString(); Type taskClass = this.taskTypeMap.get(typeOfTask); if (taskClass == null) { throw new JsonParseException("Invalid Task Definition type!"); } TaskDefinition result = this.jsonConverter.fromJson(json, taskClass); return result; }
From source file:io.hawkcd.utilities.deserializers.WsContractDeserializer.java
License:Apache License
@Override public WsContractDto deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); for (String fieldName : this.requiredFields) { if (jsonObject.get(fieldName) == null) { throw new JsonParseException("Required field not found"); }// w w w . j a v a 2 s . c om } WsContractDto result = this.jsonConverter.fromJson(json, WsContractDto.class); return result; }
From source file:io.jawg.osmcontributor.rest.mappers.JodaTimeDateTimeDeserializer.java
License:Open Source License
@Override public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { DateTime dateTime = null;//from ww w . j ava2 s .c o m if (!json.isJsonNull()) { try { dateTime = DateTime.parse(json.getAsString(), ISODateTimeFormat.dateTime()); } catch (Exception e) { Timber.e(e, "Joda-Time DateTime Transform failed."); throw new JsonParseException(e); } } return dateTime; }