use of jakarta.json.JsonObject in project JsonPath by jayway.
the class JakartaJsonProvider method proxyAll.
private JsonStructure proxyAll(JsonStructure jsonStruct) {
if (jsonStruct == null) {
return null;
} else if (jsonStruct instanceof JsonArrayProxy) {
return (JsonArray) jsonStruct;
} else if (jsonStruct instanceof JsonArray) {
List<Object> array = new ArrayList<>();
for (JsonValue v : (JsonArray) jsonStruct) {
if (v instanceof JsonStructure) {
v = proxyAll((JsonStructure) v);
}
array.add(v);
}
return new JsonArrayProxy(jsonBuilderFactory.createArrayBuilder(array).build());
} else if (jsonStruct instanceof JsonObjectProxy) {
return (JsonObject) jsonStruct;
} else if (jsonStruct instanceof JsonObject) {
Map<String, Object> map = new LinkedHashMap<>();
for (Map.Entry<String, JsonValue> e : ((JsonObject) jsonStruct).entrySet()) {
JsonValue v = e.getValue();
if (v instanceof JsonStructure) {
v = proxyAll((JsonStructure) v);
}
map.put(e.getKey(), v);
}
return new JsonObjectProxy(jsonBuilderFactory.createObjectBuilder(map).build());
} else {
throw new IllegalArgumentException();
}
}
use of jakarta.json.JsonObject in project JsonPath by jayway.
the class JakartaJsonProviderTest method an_object_can_be_read_from_bytes.
@Test
public void an_object_can_be_read_from_bytes() {
JsonObject book = using(JAKARTA_JSON_CONFIGURATION).parseUtf8(JSON_DOCUMENT.getBytes(UTF_8)).read("$.store.book[0]");
assertThat(((JsonString) book.get("author")).getChars()).isEqualTo("Nigel Rees");
}
use of jakarta.json.JsonObject in project JsonPath by jayway.
the class JakartaJsonProviderTest method an_object_can_be_read.
@Test
public void an_object_can_be_read() {
JsonObject book = using(JAKARTA_JSON_CONFIGURATION).parse(JSON_DOCUMENT).read("$.store.book[0]");
assertThat(((JsonString) book.get("author")).getChars()).isEqualTo("Nigel Rees");
}
use of jakarta.json.JsonObject in project JsonPath by json-path.
the class JakartaMappingProvider method mapImpl.
private Object mapImpl(Object source, final Type targetType) {
if (source == null || source == JsonValue.NULL) {
return null;
}
if (source == JsonValue.TRUE) {
if (Boolean.class.equals(targetType)) {
return Boolean.TRUE;
} else {
String className = targetType.toString();
throw new MappingException("JSON boolean (true) cannot be mapped to " + className);
}
}
if (source == JsonValue.FALSE) {
if (Boolean.class.equals(targetType)) {
return Boolean.FALSE;
} else {
String className = targetType.toString();
throw new MappingException("JSON boolean (false) cannot be mapped to " + className);
}
} else if (source instanceof JsonString) {
if (String.class.equals(targetType)) {
return ((JsonString) source).getChars();
} else {
String className = targetType.toString();
throw new MappingException("JSON string cannot be mapped to " + className);
}
} else if (source instanceof JsonNumber) {
JsonNumber jsonNumber = (JsonNumber) source;
if (jsonNumber.isIntegral()) {
return mapIntegralJsonNumber(jsonNumber, getRawClass(targetType));
} else {
return mapDecimalJsonNumber(jsonNumber, getRawClass(targetType));
}
}
if (source instanceof JsonArrayBuilder) {
source = ((JsonArrayBuilder) source).build();
} else if (source instanceof JsonObjectBuilder) {
source = ((JsonObjectBuilder) source).build();
}
if (source instanceof Collection) {
// this covers both List<JsonValue> and JsonArray from JSON-P spec
Class<?> rawTargetType = getRawClass(targetType);
Type targetTypeArg = getFirstTypeArgument(targetType);
Collection<Object> result = newCollectionOfType(rawTargetType);
for (Object srcValue : (Collection<?>) source) {
if (srcValue instanceof JsonObject) {
if (targetTypeArg != null) {
result.add(mapImpl(srcValue, targetTypeArg));
} else {
result.add(srcValue);
}
} else {
result.add(unwrapJsonValue(srcValue));
}
}
return result;
} else if (source instanceof JsonObject) {
if (targetType instanceof Class) {
if (jsonToClassMethod != null) {
try {
JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
return jsonToClassMethod.invoke(jsonb, jsonParser, (Class<?>) targetType);
} catch (Exception e) {
throw new MappingException(e);
}
} else {
try {
// Fallback databinding approach for JSON-B API implementations without
// explicit support for use of JsonParser in their public API. The approach
// is essentially first to serialize given value into JSON, and then bind
// it to data object of given class.
String json = source.toString();
return jsonb.fromJson(json, (Class<?>) targetType);
} catch (JsonbException e) {
throw new MappingException(e);
}
}
} else if (targetType instanceof ParameterizedType) {
if (jsonToTypeMethod != null) {
try {
JsonParser jsonParser = new JsonStructureToParserAdapter((JsonStructure) source);
return jsonToTypeMethod.invoke(jsonb, jsonParser, (Type) targetType);
} catch (Exception e) {
throw new MappingException(e);
}
} else {
try {
// Fallback databinding approach for JSON-B API implementations without
// explicit support for use of JsonParser in their public API. The approach
// is essentially first to serialize given value into JSON, and then bind
// the JSON string to data object of given type.
String json = source.toString();
return jsonb.fromJson(json, (Type) targetType);
} catch (JsonbException e) {
throw new MappingException(e);
}
}
} else {
throw new MappingException("JSON object cannot be databind to " + targetType);
}
} else {
return source;
}
}
use of jakarta.json.JsonObject in project openmq by eclipse-ee4j.
the class JSONWebSocket method processData.
@Override
protected void processData(String text) throws Exception {
if (DEBUG) {
logger.log(logger.INFO, toString() + ".processData(text=" + text + ")");
}
try {
JsonReader jsonReader = Json.createReader(new StringReader(text));
JsonObject jo = jsonReader.readObject();
String command = jo.getString(JsonMessage.Key.COMMAND);
JsonObject headers = jo.getJsonObject(JsonMessage.Key.HEADERS);
JsonObject body = jo.getJsonObject(JsonMessage.Key.BODY);
StompFrameMessage frame = StompFrameMessageImpl.getFactory().newStompFrameMessage(StompFrameMessage.Command.valueOf(command), logger);
Iterator<String> itr = headers.keySet().iterator();
String key;
String val;
while (itr.hasNext()) {
key = itr.next();
val = headers.getString(key);
if (val != null) {
frame.addHeader(key, val);
}
}
if (body != null) {
JsonString btype = body.getJsonString(JsonMessage.BodySubKey.TYPE);
if (btype == null || btype.getString().equals(JsonMessage.BODY_TYPE_TEXT)) {
JsonString msg = body.getJsonString(JsonMessage.BodySubKey.TEXT);
if (msg != null) {
frame.setBody(msg.getString().getBytes("UTF-8"));
}
} else if (btype.getString().equals(JsonMessage.BODY_TYPE_BYTES)) {
JsonString enc = body.getJsonString("encoder");
if (enc == null || enc.getString().equals(JsonMessage.ENCODER_BASE64)) {
JsonString msg = body.getJsonString(JsonMessage.BodySubKey.TEXT);
if (msg != null) {
byte[] bytes = null;
if (base64Class == null) {
BASE64Decoder decoder = new BASE64Decoder();
bytes = decoder.decodeBuffer(msg.getString());
} else {
Method gm = base64Class.getMethod("getDecoder", (new Class[] {}));
Object o = gm.invoke(null);
Method dm = o.getClass().getMethod("decode", (new Class[] { String.class }));
bytes = (byte[]) dm.invoke(o, msg.getString());
}
frame.setBody(bytes);
frame.addHeader(StompFrameMessage.CommonHeader.CONTENTLENGTH, String.valueOf(bytes.length));
}
} else {
throw new IOException("encoder " + enc + " not supported");
}
} else {
throw new IOException("body type:" + btype + " not supported");
}
}
dispatchMessage((StompFrameMessageImpl) frame);
} catch (Exception e) {
logger.logStack(logger.ERROR, e.getMessage(), e);
sendFatalError(e);
}
}
Aggregations