use of jakarta.json.JsonArrayBuilder 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.JsonArrayBuilder in project dash-licenses by eclipse.
the class EclipseFoundationSupport method buildRequestPayload.
private JsonObject buildRequestPayload(Collection<IContentId> ids) {
JsonObjectBuilder request = Json.createObjectBuilder();
var projectId = settings.getProjectId();
if (projectId != null) {
logger.debug("Querying for project {}.", projectId);
request.add("project", projectId);
}
JsonArrayBuilder builder = Json.createBuilderFactory(null).createArrayBuilder();
ids.stream().forEach(id -> builder.add(id.toString()));
request.add("dependencies", builder);
return request.build();
}
use of jakarta.json.JsonArrayBuilder in project dash-licenses by eclipse.
the class EclipseFoundationSupport method buildJsonPayload.
private JsonArray buildJsonPayload(Collection<IContentId> ids) {
JsonArrayBuilder builder = Json.createBuilderFactory(null).createArrayBuilder();
ids.stream().forEach(id -> builder.add(id.toString()));
return builder.build();
}
use of jakarta.json.JsonArrayBuilder in project dash-licenses by eclipse.
the class JsonUtils method toJson.
public static String toJson(Collection<IContentId> ids) {
// TODO Consider generalizing
JsonArrayBuilder builder = Json.createBuilderFactory(null).createArrayBuilder();
ids.stream().forEach(id -> builder.add(id.toString()));
String json = builder.build().toString();
return json;
}
use of jakarta.json.JsonArrayBuilder in project glassfish-hk2 by eclipse-ee4j.
the class JsonParser method createJsonObject.
@SuppressWarnings("unchecked")
private JsonObject createJsonObject(BaseHK2JAXBBean bean, JsonObjectBuilder builder) {
if (bean == null) {
return builder.build();
}
ModelImpl model = bean._getModel();
Map<QName, ChildDescriptor> allChildren = model.getAllChildrenDescriptors();
for (Map.Entry<QName, ChildDescriptor> entry : allChildren.entrySet()) {
QName keyNameQName = entry.getKey();
String keyName = keyNameQName.getLocalPart();
if (!bean._isSet(keyName))
continue;
Object value = bean._getProperty(XmlService.DEFAULT_NAMESPACE, keyName);
ParentedModel parented = entry.getValue().getParentedModel();
if (parented != null) {
if (ChildType.DIRECT.equals(parented.getChildType())) {
if (value != null) {
builder = builder.add(keyName, createJsonObject((BaseHK2JAXBBean) value, Json.createObjectBuilder()));
}
} else if (ChildType.LIST.equals(parented.getChildType())) {
List<BaseHK2JAXBBean> list = (List<BaseHK2JAXBBean>) value;
if (list != null && !list.isEmpty()) {
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (BaseHK2JAXBBean item : list) {
JsonObjectBuilder childBuilder = Json.createObjectBuilder();
JsonObject obj = createJsonObject(item, childBuilder);
jsonArray.add(obj);
}
builder.add(keyName, jsonArray);
}
} else if (ChildType.ARRAY.equals(parented.getChildType())) {
int length = Array.getLength(value);
if (length > 0) {
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (int lcv = 0; lcv < length; lcv++) {
BaseHK2JAXBBean item = (BaseHK2JAXBBean) Array.get(value, lcv);
JsonObjectBuilder childBuilder = Json.createObjectBuilder();
JsonObject obj = createJsonObject(item, childBuilder);
jsonArray.add(obj);
}
builder.add(keyName, jsonArray);
}
} else {
throw new AssertionError("Unknown childType " + parented.getChildType());
}
} else {
if (value == null) {
builder.addNull(keyName);
} else if (value instanceof Integer) {
builder.add(keyName, ((Integer) value).intValue());
} else if (value instanceof Long) {
builder.add(keyName, ((Long) value).longValue());
} else if (value instanceof Boolean) {
builder.add(keyName, ((Boolean) value).booleanValue());
} else {
builder.add(keyName, value.toString());
}
}
}
return builder.build();
}
Aggregations