/** * Always return a JSON Writer. * * @return The JSON writer to use. * * @throws UnsupportedOperationException if no provider could be found. */ private static synchronized JsonGeneratorFactory loadFactory() { try { // This method does not return null, it throws a JsonException instead JsonProvider provider = JsonProvider.provider(); LOGGER.debug("JSON Provider found using {}", provider.getClass().getName()); // XXX: We could supply configuration for the factory return provider.createGeneratorFactory(Collections.emptyMap()); } catch (JsonException ex) { LOGGER.warn("JSON Provider not found: {}", ex.getMessage()); throw new UnsupportedOperationException("Unable to find suitable provider"); } }
@Override public void parse(InputStream ins, ContentCreator contentCreator) throws IOException, RepositoryException { try { String jsonString = toString(ins).trim(); if (!jsonString.startsWith("{")) { jsonString = "{" + jsonString + "}"; } Map<String, Object> config = new HashMap<>(); config.put("org.apache.johnzon.supports-comments", true); JsonObject json = Json.createReaderFactory(config).createReader(new StringReader(tickToDoubleQuote(jsonString))).readObject(); this.createNode(null, json, contentCreator); } catch (JsonException je) { throw (IOException) new IOException(je.getMessage()).initCause(je); } }
@Override public ByteBuffer serialize(JsonStructure value, ProtocolVersion protocolVersion) throws InvalidTypeException { if (value == null) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { JsonWriter writer = writerFactory.createWriter(baos); writer.write(value); return ByteBuffer.wrap(baos.toByteArray()); } catch (JsonException e) { throw new InvalidTypeException(e.getMessage(), e); } finally { try { baos.close(); } catch (IOException e) { // cannot happen throw new InvalidTypeException(e.getMessage(), e); } } }
@Override public String format(JsonStructure value) throws InvalidTypeException { if (value == null) return "NULL"; String json; StringWriter sw = new StringWriter(); try { JsonWriter writer = writerFactory.createWriter(sw); writer.write(value); json = sw.toString(); } catch (JsonException e) { throw new InvalidTypeException(e.getMessage(), e); } finally { try { sw.close(); } catch (IOException e) { // cannot happen throw new InvalidTypeException(e.getMessage(), e); } } return ParseUtils.quote(json); }
@Override @SuppressWarnings("unchecked") public JsonStructure parse(String value) throws InvalidTypeException { if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) return null; if (!ParseUtils.isQuoted(value)) throw new InvalidTypeException("JSON strings must be enclosed by single quotes"); String json = ParseUtils.unquote(value); StringReader sr = new StringReader(json); try { JsonReader reader = readerFactory.createReader(sr); return reader.read(); } catch (JsonException e) { throw new InvalidTypeException(e.getMessage(), e); } finally { sr.close(); } } }
@Override @SuppressWarnings("unchecked") public JsonStructure deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { if (bytes == null) return null; ByteArrayInputStream bais = new ByteArrayInputStream(Bytes.getArray(bytes)); try { JsonReader reader = readerFactory.createReader(bais); return reader.read(); } catch (JsonException e) { throw new InvalidTypeException(e.getMessage(), e); } finally { try { bais.close(); } catch (IOException e) { // cannot happen throw new InvalidTypeException(e.getMessage(), e); } } }
throw new IOException("Unable to Encrypt Message " + e.getMessage()); } catch (JsonException e) { throw new IOException("Unable to Encrypt Message " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new IOException("Unable to Encrypt Message " + e.getMessage());