use of javax.json.JsonObjectBuilder in project sling by apache.
the class JsonObjectCreator method create.
/** Dump given resource in JSON, optionally recursing into its objects */
private static JsonObjectBuilder create(final Resource resource, final int currentRecursionLevel, final int maxRecursionLevels) {
final ValueMap valueMap = resource.adaptTo(ValueMap.class);
final Map propertyMap = (valueMap != null) ? valueMap : resource.adaptTo(Map.class);
final JsonObjectBuilder obj = Json.createObjectBuilder();
if (propertyMap == null) {
// no map available, try string
final String value = resource.adaptTo(String.class);
if (value != null) {
// single value property or just plain String resource or...
obj.add(ResourceUtil.getName(resource), value.toString());
} else {
// Try multi-value "property"
final String[] values = resource.adaptTo(String[].class);
if (values != null) {
JsonArrayBuilder builder = Json.createArrayBuilder();
for (String v : values) {
builder.add(v);
}
obj.add(ResourceUtil.getName(resource), builder);
}
}
} else {
@SuppressWarnings("unchecked") final Iterator<Map.Entry> props = propertyMap.entrySet().iterator();
// the node's actual properties
while (props.hasNext()) {
final Map.Entry prop = props.next();
if (prop.getValue() != null) {
createProperty(obj, valueMap, prop.getKey().toString(), prop.getValue());
}
}
}
// the child nodes
if (recursionLevelActive(currentRecursionLevel, maxRecursionLevels)) {
final Iterator<Resource> children = ResourceUtil.listChildren(resource);
while (children.hasNext()) {
final Resource n = children.next();
createSingleResource(n, obj, currentRecursionLevel, maxRecursionLevels);
}
}
return obj;
}
use of javax.json.JsonObjectBuilder in project sling by apache.
the class JsonRenderer method skipChildObject.
/** Decide whether o must be skipped and added to a, when rendering a JSONObject */
private boolean skipChildObject(JsonArrayBuilder a, Options opt, String key, Object value) {
if (opt.arraysForChildren && (value instanceof JsonObject)) {
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add(opt.childNameKey, key);
for (Map.Entry<String, JsonValue> entry : ((JsonObject) value).entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
a.add(builder);
return true;
}
return false;
}
use of javax.json.JsonObjectBuilder in project knime-core by knime.
the class PMMLGeneralRegressionTranslator method exportTo.
/**
* {@inheritDoc}
*/
@Override
public SchemaType exportTo(final PMMLDocument pmmlDoc, final PMMLPortObjectSpec spec) {
m_nameMapper = new DerivedFieldMapper(pmmlDoc);
GeneralRegressionModel reg = pmmlDoc.getPMML().addNewGeneralRegressionModel();
final JsonObjectBuilder jsonBuilder = Json.createObjectBuilder();
if (!m_content.getVectorLengths().isEmpty()) {
LocalTransformations localTransformations = reg.addNewLocalTransformations();
for (final Entry<? extends String, ? extends Integer> entry : m_content.getVectorLengths().entrySet()) {
DataColumnSpec columnSpec = spec.getDataTableSpec().getColumnSpec(entry.getKey());
if (columnSpec != null) {
final DataType type = columnSpec.getType();
final DataColumnProperties props = columnSpec.getProperties();
final boolean bitVector = type.isCompatible(BitVectorValue.class) || (type.isCompatible(StringValue.class) && props.containsProperty("realType") && "BitVector".equals(props.getProperty("realType")));
final boolean byteVector = type.isCompatible(ByteVectorValue.class) || (type.isCompatible(StringValue.class) && props.containsProperty("realType") && "ByteVector".equals(props.getProperty("realType")));
final String lengthAsString;
final int width;
if (byteVector) {
lengthAsString = "3";
width = 4;
} else if (bitVector) {
lengthAsString = "1";
width = 1;
} else {
throw new UnsupportedOperationException("Not supported type: " + type + " for column: " + columnSpec);
}
for (int i = 0; i < entry.getValue().intValue(); ++i) {
final DerivedField derivedField = localTransformations.addNewDerivedField();
derivedField.setOptype(OPTYPE.CONTINUOUS);
derivedField.setDataType(DATATYPE.INTEGER);
derivedField.setName(entry.getKey() + "[" + i + "]");
Apply apply = derivedField.addNewApply();
apply.setFunction("substring");
apply.addNewFieldRef().setField(entry.getKey());
Constant from = apply.addNewConstant();
from.setDataType(DATATYPE.INTEGER);
from.setStringValue(bitVector ? Long.toString(entry.getValue().longValue() - i) : Long.toString(i * width + 1L));
Constant length = apply.addNewConstant();
length.setDataType(DATATYPE.INTEGER);
length.setStringValue(lengthAsString);
}
}
jsonBuilder.add(entry.getKey(), entry.getValue().intValue());
}
}
// PMMLPortObjectSpecCreator newSpecCreator = new PMMLPortObjectSpecCreator(spec);
// newSpecCreator.addPreprocColNames(m_content.getVectorLengths().entrySet().stream()
// .flatMap(
// e -> IntStream.iterate(0, o -> o + 1).limit(e.getValue()).mapToObj(i -> e.getKey() + "[" + i + "]"))
// .collect(Collectors.toList()));
PMMLMiningSchemaTranslator.writeMiningSchema(spec, reg);
// if (!m_content.getVectorLengths().isEmpty()) {
// Extension miningExtension = reg.getMiningSchema().addNewExtension();
// miningExtension.setExtender(EXTENDER);
// miningExtension.setName(VECTOR_COLUMNS_WITH_LENGTH);
// miningExtension.setValue(jsonBuilder.build().toString());
// }
reg.setModelType(getPMMLRegModelType(m_content.getModelType()));
reg.setFunctionName(getPMMLMiningFunction(m_content.getFunctionName()));
String algorithmName = m_content.getAlgorithmName();
if (algorithmName != null && !algorithmName.isEmpty()) {
reg.setAlgorithmName(algorithmName);
}
String modelName = m_content.getModelName();
if (modelName != null && !modelName.isEmpty()) {
reg.setModelName(modelName);
}
String targetReferenceCategory = m_content.getTargetReferenceCategory();
if (targetReferenceCategory != null && !targetReferenceCategory.isEmpty()) {
reg.setTargetReferenceCategory(targetReferenceCategory);
}
if (m_content.getOffsetValue() != null) {
reg.setOffsetValue(m_content.getOffsetValue());
}
// add parameter list
ParameterList paramList = reg.addNewParameterList();
for (PMMLParameter p : m_content.getParameterList()) {
Parameter param = paramList.addNewParameter();
param.setName(p.getName());
String label = p.getLabel();
if (label != null) {
param.setLabel(m_nameMapper.getDerivedFieldName(label));
}
}
// add factor list
FactorList factorList = reg.addNewFactorList();
for (PMMLPredictor p : m_content.getFactorList()) {
Predictor predictor = factorList.addNewPredictor();
predictor.setName(m_nameMapper.getDerivedFieldName(p.getName()));
}
// add covariate list
CovariateList covariateList = reg.addNewCovariateList();
for (PMMLPredictor p : m_content.getCovariateList()) {
Predictor predictor = covariateList.addNewPredictor();
predictor.setName(m_nameMapper.getDerivedFieldName(p.getName()));
}
// add PPMatrix
PPMatrix ppMatrix = reg.addNewPPMatrix();
for (PMMLPPCell p : m_content.getPPMatrix()) {
PPCell cell = ppMatrix.addNewPPCell();
cell.setValue(p.getValue());
cell.setPredictorName(m_nameMapper.getDerivedFieldName(p.getPredictorName()));
cell.setParameterName(p.getParameterName());
String targetCategory = p.getTargetCategory();
if (targetCategory != null && !targetCategory.isEmpty()) {
cell.setTargetCategory(targetCategory);
}
}
// add CovMatrix
if (m_content.getPCovMatrix().length > 0) {
PCovMatrix pCovMatrix = reg.addNewPCovMatrix();
for (PMMLPCovCell p : m_content.getPCovMatrix()) {
PCovCell covCell = pCovMatrix.addNewPCovCell();
covCell.setPRow(p.getPRow());
covCell.setPCol(p.getPCol());
String tCol = p.getTCol();
String tRow = p.getTRow();
if (tRow != null || tCol != null) {
covCell.setTRow(tRow);
covCell.setTCol(tCol);
}
covCell.setValue(p.getValue());
String targetCategory = p.getTargetCategory();
if (targetCategory != null && !targetCategory.isEmpty()) {
covCell.setTargetCategory(targetCategory);
}
}
}
// add ParamMatrix
ParamMatrix paramMatrix = reg.addNewParamMatrix();
for (PMMLPCell p : m_content.getParamMatrix()) {
PCell pCell = paramMatrix.addNewPCell();
String targetCategory = p.getTargetCategory();
if (targetCategory != null) {
pCell.setTargetCategory(targetCategory);
}
pCell.setParameterName(p.getParameterName());
pCell.setBeta(p.getBeta());
Integer df = p.getDf();
if (df != null) {
pCell.setDf(BigInteger.valueOf(df));
}
}
return GeneralRegressionModel.type;
}
use of javax.json.JsonObjectBuilder in project knime-core by knime.
the class OpenAPIDefinitionGenerator method createParametersDescription.
private static JsonObject createParametersDescription(final Map<String, ExternalNodeData> nodes) {
JsonObjectBuilder root = Json.createObjectBuilder();
if (!nodes.isEmpty()) {
JsonObjectBuilder properties = Json.createObjectBuilder();
for (Map.Entry<String, ExternalNodeData> e : nodes.entrySet()) {
if (e.getValue().getJSONValue() != null) {
JsonObjectBuilder input = translateToSchema(e.getValue().getJSONValue());
e.getValue().getDescription().ifPresent(d -> input.add("description", d));
input.add("example", e.getValue().getJSONValue());
properties.add(e.getKey(), input);
}
}
root.add("type", "object");
root.add("properties", properties);
}
return root.build();
}
use of javax.json.JsonObjectBuilder in project opentheso by miledrousset.
the class ReadJsonTest method getJsonData.
// TODO add test methods here.
// The methods must be annotated with annotation @Test. For example:
//
/* @Test
public void readJson1() {
/*
String personJSONData
= " {"
+ " \"name\": \"Jack\", "
+ " \"age\" : 13, "
+ " \"isMarried\" : false, "
+ " \"address\": { "
+ " \"street\": \"#1234, Main Street\", "
+ " \"zipCode\": \"123456\" "
+ " }, "
+ " \"phoneNumbers\": [\"011-111-1111\", \"11-111-1111\"] "
+ " }";
JsonReader reader = Json.createReader(new StringReader(personJSONData));
JsonObject personObject = reader.readObject();
reader.close();
System.out.println("Name : " + personObject.getString("name"));
System.out.println("Age : " + personObject.getInt("age"));
System.out.println("Married: " + personObject.getBoolean("isMarried"));
JsonObject addressObject = personObject.getJsonObject("address");
System.out.println("Address: ");
System.out.println(addressObject.getString("street"));
System.out.println(addressObject.getString("zipCode"));
System.out.println("Phone : ");
JsonArray phoneNumbersArray = personObject.getJsonArray("phoneNumbers");
for (JsonValue jsonValue : phoneNumbersArray) {
System.out.println(jsonValue.toString());
}
*/
// }*/
/* @Test
private void readJson2() {
String total = " {\"content\":[{\"nb_notices\":\"7\"}],\"debug\":\"\",\"error\":0}\" ";
JsonReader reader = Json.createReader(new StringReader(total));
JsonObject personObject = reader.readObject();
reader.close();
JsonArray values = personObject.getJsonArray("content");
for (int i = 0; i < values.size(); i++) {
try {
JsonObject item = values.getJsonObject(i);
String name = item.getString("nb_notices");
int nb = Integer.parseInt(name);
System.out.println("Total des notices = " + nb);
} catch (JsonException e) {
System.out.println(e.toString());
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
/* for (JsonObject jsonValue : values) {
System.out.println(jsonValue.toString());
}*/
/* JsonArray personArray = reader.readArray();
for (JsonValue jsonVal : personArray) {
System.out.println(jsonVal.getValueType() + " - "
+ ((JsonObject) jsonVal).getString("nb_notices"));
}
*/
// }*/
@Test
public void getJsonData() {
String urlResource = "http://test.mom.fr";
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("index", "1");
builder.add("type", "URL");
// Objet dans l'Objet
JsonObjectBuilder job = Json.createObjectBuilder();
job.add("format", "string");
job.add("value", urlResource);
builder.add("data", job.build());
builder.add("ttl", "86400");
builder.add("permissions", "1110");
String test = builder.build().toString();
System.err.println(test);
}
Aggregations