Java Code Examples for javax.json.JsonValue#toString()

The following examples show how to use javax.json.JsonValue#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserView.java    From javaee8-cookbook with Apache License 2.0 7 votes vote down vote up
private void loadFromStructure() {
    JsonStructure structure = BUILDERFACTORY.createObjectBuilder()
            .add("name", "User1")
            .add("email", "[email protected]")
            .add("profiles", BUILDERFACTORY.createArrayBuilder()
                    .add(BUILDERFACTORY.createObjectBuilder()
                            .add("id", "1")
                            .add("name", "Profile1"))
                    .add(BUILDERFACTORY.createObjectBuilder()
                            .add("id", "2")
                            .add("name", "Profile2")))
            .build();
    fromStructure = jsonbBuilder.toJson(structure);

    JsonPointer pointer = Json.createPointer("/profiles");
    JsonValue value = pointer.getValue(structure);
    fromJpointer = value.toString();
}
 
Example 2
Source File: JwtClaimsBuilderImpl.java    From smallrye-jwt with Apache License 2.0 6 votes vote down vote up
private static Object convertJsonValue(JsonValue jsonValue) {
    if (jsonValue instanceof JsonString) {
        String jsonString = jsonValue.toString();
        return jsonString.toString().substring(1, jsonString.length() - 1);
    } else if (jsonValue instanceof JsonNumber) {
        JsonNumber jsonNumber = (JsonNumber) jsonValue;
        if (jsonNumber.isIntegral()) {
            return jsonNumber.longValue();
        } else {
            return jsonNumber.doubleValue();
        }
    } else if (jsonValue == JsonValue.TRUE) {
        return true;
    } else if (jsonValue == JsonValue.FALSE) {
        return false;
    } else {
        return null;
    }
}
 
Example 3
Source File: Json2Xml.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public String getNodeText(XSElementDeclaration elementDeclaration, JsonValue node) {
	String result;
	if (node instanceof JsonString) {
		result=((JsonString)node).getString();
	} else if (node instanceof JsonStructure) { // this happens when override key is present without a value
		result=null; 
	} else { 
		result=node.toString();
	}
	if ("{}".equals(result)) {
		result="";
	}
	if (log.isTraceEnabled()) log.trace("getText() node ["+ToStringBuilder.reflectionToString(node)+"] = ["+result+"]");
	return result;
}
 
Example 4
Source File: SchemalessJsonToIndexedRecord.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private Schema guessSchema(final String recordName, final JsonValue element) {
    switch (element.getValueType()) {
    case STRING:
        return STRING;
    case NUMBER:
        final Number number = JsonNumber.class.cast(element).numberValue();
        if (Long.class.isInstance(number)) {
            return LONG;
        }
        if (Integer.class.isInstance(number)) {
            return INT;
        }
        return DOUBLE;
    case FALSE:
    case TRUE:
        return BOOLEAN;
    case NULL:
        return NULL;
    case OBJECT:
        final Schema record = Schema.createRecord(recordName, null, NAMESPACE, false);
        record
                .setFields(element
                        .asJsonObject()
                        .entrySet()
                        .stream()
                        .map(it -> new Schema.Field(it.getKey(),
                                guessSchema(buildNextName(recordName, it.getKey()), it.getValue()), null, null))
                        .collect(toList()));
        return record;
    case ARRAY:
        final JsonArray array = element.asJsonArray();
        if (!array.isEmpty()) {
            return Schema.createArray(guessSchema(buildNextName(recordName, "Array"), array.iterator().next()));
        }
        return Schema.createArray(Schema.create(Schema.Type.NULL));
    default:
        throw new IllegalArgumentException("Unsupported: " + element.toString());
    }
}
 
Example 5
Source File: FeatureServiceImpl.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
private FeatureBundle[] getBundles(JsonObject json) {
    JsonArray ja = json.getJsonArray("bundles");
    if (ja == null)
        return new FeatureBundle[] {};

    List<FeatureBundle> bundles = new ArrayList<>();

    for (JsonValue val : ja) {
        if (val.getValueType() == JsonValue.ValueType.OBJECT) {
            JsonObject jo = val.asJsonObject();
            String bid = jo.getString("id");
            FeatureBundleBuilder builder = builderFactory.newBundleBuilder(ID.fromMavenID(bid));

            for (Map.Entry<String, JsonValue> entry : jo.entrySet()) {
                if (entry.getKey().equals("id"))
                    continue;

                JsonValue value = entry.getValue();

                Object v;
                switch (value.getValueType()) {
                case NUMBER:
                    v = ((JsonNumber) value).longValueExact();
                    break;
                case STRING:
                    v = ((JsonString) value).getString();
                    break;
                default:
                    v = value.toString();
                }
                builder.addMetadata(entry.getKey(), v);
            }
            bundles.add(builder.build());
        }
    }

    return bundles.toArray(new FeatureBundle[0]);
}
 
Example 6
Source File: JsonpRuntime.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toString(JsonValue str) {
  if (str.getValueType() == STRING) {
    return ((JsonString) str).getString();
  } else {
    return str.toString();
  }
}
 
Example 7
Source File: AdminServlet.java    From sample.microservices.12factorapp with Apache License 2.0 5 votes vote down vote up
private String parse(String stats) throws IOException {
    // Convert string to jsonObject
    InputStream is = new ByteArrayInputStream(stats.getBytes("UTF-8"));
    JsonReader reader = Json.createReader(is);
    String output = "";
    try {
        JsonArray jsonArray = reader.readArray();
        JsonObject jsonObject = jsonArray.getJsonObject(0);
        JsonObject topLevelValue = (JsonObject) jsonObject.get("value");
        JsonObject value = (JsonObject) topLevelValue.get("value");
        JsonValue currentValue = value.get("currentValue");
        JsonValue desc = value.get("description");
        output = "Stats:" + desc.toString() + ": " + currentValue.toString();
    } catch (JsonException e) {
        reader.close();
        is.close();
        if (e.getMessage().equals("Cannot read JSON array, found JSON object")) {
            output = "MXBean not created yet, the application must be accessed at least "
                     + "once to get statistics";
        } else {
            output = "A JSON Exception occurred: " + e.getMessage();
        }
    }
    reader.close();
    is.close();
    return output;
}
 
Example 8
Source File: FirstNameAdapter.java    From Java-EE-8-Sampler with MIT License 4 votes vote down vote up
@Override
public String adaptFromJson(JsonValue json) {
    return json.toString();
}
 
Example 9
Source File: FeatureServiceImpl.java    From sling-whiteboard with Apache License 2.0 4 votes vote down vote up
private FeatureConfiguration[] getConfigurations(JsonObject json) {
    JsonObject jo = json.getJsonObject("configurations");
    if (jo == null)
        return new FeatureConfiguration[] {};

    List<FeatureConfiguration> configs = new ArrayList<>();

    for (Map.Entry<String, JsonValue> entry : jo.entrySet()) {

        String p = entry.getKey();
        String factoryPid = null;
        int idx = p.indexOf('~');
        if (idx > 0) {
            factoryPid = p.substring(0, idx);
            p = p.substring(idx + 1);
        }

        FeatureConfigurationBuilder builder;
        if (factoryPid == null) {
            builder = builderFactory.newConfigurationBuilder(p);
        } else {
            builder = builderFactory.newConfigurationBuilder(factoryPid, p);
        }

        JsonObject values = entry.getValue().asJsonObject();
        for (Map.Entry<String, JsonValue> value : values.entrySet()) {
            JsonValue val = value.getValue();

            Object v;
            switch (val.getValueType()) {
            case TRUE:
                v = true;
                break;
            case FALSE:
                v = false;
                break;
            case NUMBER:
                v = ((JsonNumber) val).longValueExact();
                break;
            case STRING:
                v = ((JsonString) val).getString();
                break;
            default:
                v = val.toString();

                // TODO object types, arrays, and requested type conversions
            }
            builder.addValue(value.getKey(), v);
        }
        configs.add(builder.build());
    }

    return configs.toArray(new FeatureConfiguration[] {});
}
 
Example 10
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static String getJsonValueAsNumberString(JsonValue value) {
    return (value != null && value.getValueType() == ValueType.NUMBER) ? value.toString() : null;
}
 
Example 11
Source File: SolrEntityIndexerMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
private void indexJson( SolrInputDocument input, Object object )
{
    if( object instanceof JsonArray )
    {
        JsonArray array = (JsonArray) object;
        for( int i = 0; i < array.size(); i++ )
        {
            indexJson( input, array.get( i ) );
        }
    }
    else
    {
        JsonObject jsonObject = (JsonObject) object;
        for( String name : jsonObject.keySet() )
        {
            JsonValue jsonValue = jsonObject.get( name );
            if( jsonValue.getValueType() == JsonValue.ValueType.OBJECT
                || jsonValue.getValueType() == JsonValue.ValueType.ARRAY )
            {
                indexJson( input, jsonValue );
            }
            else
            {
                SchemaField field = indexedFields.get( name );
                if( field != null )
                {
                    Object value;
                    switch( jsonValue.getValueType() )
                    {
                        case NULL:
                            value = null;
                            break;
                        case STRING:
                            value = ( (JsonString) jsonValue ).getString();
                            break;
                        case NUMBER:
                            JsonNumber jsonNumber = (JsonNumber) jsonValue;
                            value = jsonNumber.isIntegral() ? jsonNumber.longValue() : jsonNumber.doubleValue();
                            break;
                        case TRUE:
                            value = Boolean.TRUE;
                            break;
                        case FALSE:
                            value = Boolean.FALSE;
                            break;
                        default:
                            value = jsonValue.toString();
                    }
                    input.addField( name, value );
                }
            }
        }
    }
}
 
Example 12
Source File: JavaxJson.java    From attic-polygene-java with Apache License 2.0 2 votes vote down vote up
/**
 * Get a {@link String} out of a {@link JsonValue}.
 *
 * @param jsonValue the JSON value
 * @return the String
 */
public static String asString( JsonValue jsonValue )
{
    return jsonValue instanceof JsonString ? ( (JsonString) jsonValue ).getString() : jsonValue.toString();
}