List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:io.kodokojo.endpoint.dto.WebSocketMessageGsonAdapter.java
License:Open Source License
@Override public WebSocketMessage deserialize(JsonElement input, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject json = (JsonObject) input; JsonPrimitive jsonEntity = json.getAsJsonPrimitive("entity"); if (jsonEntity == null) { throw new JsonParseException("entity attribute expected."); }//from w w w . j ava 2 s.co m String entity = jsonEntity.getAsString(); JsonPrimitive jsonAction = json.getAsJsonPrimitive("action"); if (jsonAction == null) { throw new JsonParseException("action attribute expected."); } String action = jsonAction.getAsString(); JsonObject jsonData = json.getAsJsonObject("data"); if (jsonData == null) { throw new JsonParseException("data attribute expected."); } return new WebSocketMessage(entity, action, jsonData); }
From source file:io.morea.handy.android.DateSerializer.java
License:Apache License
@Override public Date deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { try {// ww w .j a va 2 s . com return Util.dateFromIso8601(jsonElement.getAsJsonPrimitive().getAsString()); } catch (ParseException e) { throw new JsonParseException(e); } }
From source file:io.motown.operatorapi.json.commands.JsonCommandService.java
License:Apache License
public void handleCommand(String chargingStationId, String jsonCommand, UserIdentity userIdentity) throws UserIdentityUnauthorizedException { JsonArray commandAsArray = gson.fromJson(jsonCommand, JsonArray.class); if (commandAsArray.size() != COMMAND_ARRAY_SIZE) { throw new JsonParseException("API command must be a JSON array with two elements"); }/*ww w. j a va 2s .c om*/ String commandName = commandAsArray.get(COMMAND_NAME_INDEX).getAsString(); JsonObject commandPayloadAsObject = commandAsArray.get(COMMAND_PAYLOAD_INDEX).getAsJsonObject(); JsonCommandHandler commandHandler = getCommandHandler(commandName); IdentityContext identityContext = new IdentityContext(new TypeBasedAddOnIdentity(ADD_ON_TYPE, addOnId), userIdentity); commandHandler.handle(chargingStationId, commandPayloadAsObject, identityContext); }
From source file:io.motown.operatorapi.json.commands.JsonCommandService.java
License:Apache License
private JsonCommandHandler getCommandHandler(String commandName) { for (JsonCommandHandler commandHandler : this.jsonCommandHandlers) { if (commandName.equals(commandHandler.getCommandName())) { return commandHandler; }/*from www. j a v a2 s .c om*/ } throw new JsonParseException( String.format("No command handler is configured for handling [%s].", commandName)); }
From source file:io.motown.operatorapi.json.gson.deserializer.AccessibilityTypeAdapterDeserializer.java
License:Apache License
@Override public Accessibility deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { if (!json.isJsonPrimitive()) { throw new JsonParseException("Accessibility must be a JSON string"); }//from w w w.ja v a 2 s .com return Accessibility.fromValue(json.getAsString()); }
From source file:io.motown.operatorapi.json.gson.deserializer.EvseIdTypeAdapterDeserializer.java
License:Apache License
@Override public EvseId deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { if (!json.isJsonPrimitive()) { throw new JsonParseException("EvseId must be a JSON primitive"); }// w w w . j a va2 s. co m int evseId; try { evseId = json.getAsInt(); } catch (ClassCastException | IllegalStateException | NumberFormatException e) { throw new JsonParseException("EvseId must be a JSON integer", e); } return new EvseId(evseId); }
From source file:io.ucoin.ucoinj.core.client.model.bma.gson.IdentityTypeAdapter.java
License:Open Source License
@Override public BlockchainBlock.Identity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String identityStr = json.getAsString(); if (StringUtils.isBlank(identityStr)) { return null; }/*from w w w .java2 s . c om*/ String[] identityParts = identityStr.split(":"); if (identityParts.length != 4) { throw new JsonParseException( String.format("Bad format for BlockchainBlock.Identity. Should have 4 parts, but found %s.", identityParts.length)); } BlockchainBlock.Identity result = new BlockchainBlock.Identity(); int i = 0; result.setPubkey(identityParts[i++]); result.setSignature(identityParts[i++]); result.setTimestamp(Integer.parseInt(identityParts[i++])); result.setUid(identityParts[i++]); return result; }
From source file:io.ucoin.ucoinj.core.client.model.bma.gson.JoinerTypeAdapter.java
License:Open Source License
@Override public BlockchainBlock.Joiner deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String identityStr = json.getAsString(); if (StringUtils.isBlank(identityStr)) { return null; }/*www . j a va 2 s. c o m*/ String[] identityParts = identityStr.split(":"); if (identityParts.length != 6) { throw new JsonParseException( String.format("Bad format for BlockchainBlock.Identity. Should have 6 parts, but found %s.", identityParts.length)); } BlockchainBlock.Joiner result = new BlockchainBlock.Joiner(); int i = 0; result.setPubkey(identityParts[i++]); result.setSignature(identityParts[i++]); result.setNumber(identityParts[i++]); result.setHash(identityParts[i++]); result.setTimestamp(Integer.parseInt(identityParts[i++])); result.setUid(identityParts[i++]); return result; }
From source file:io.ucoin.ucoinj.core.client.model.bma.gson.JsonArrayParser.java
License:Open Source License
public List<String> getValuesAsList(String jsonArray) throws JsonParseException { ParserState state = ParserState.READING_ARRAY; List<String> result = new ArrayList<String>(); StringBuilder currentObject = null; int i = 0;//from w ww . j a v a 2s . c o m int parenthesisBalance = 0; for (char c : jsonArray.toCharArray()) { switch (c) { case '{': { if (state == ParserState.READING_ARRAY) { state = ParserState.READING_OBJECT; currentObject = new StringBuilder(); } parenthesisBalance++; currentObject.append(c); break; } case '}': { if (state == ParserState.READING_ARRAY) { throw new JsonParseException("unexpected '}' at " + i); } else { currentObject.append(c); parenthesisBalance--; if (parenthesisBalance == 0) { state = ParserState.READING_ARRAY; result.add(currentObject.toString()); } } break; } default: { if (state == ParserState.READING_OBJECT) { currentObject.append(c); } } } i++; } return result; }
From source file:io.vertigo.vega.engines.webservice.json.UTCDateUtil.java
License:Apache License
/** * Parse Utc date string to date/*from w w w . ja v a 2 s . c o m*/ * @param inputDate Utc date string * @return date */ public static Date parse(final String inputDate) { final boolean isTruncatedDate = isTruncatedDate(inputDate); for (final String format : INPUT_DATE_FORMATS) { try { return createDateFormat(format, isTruncatedDate).parse(inputDate); } catch (final ParseException e) { //nothing } } throw new JsonParseException("Unsupported Date format " + inputDate); }