javax.json.JsonValue.ValueType Java Examples

The following examples show how to use javax.json.JsonValue.ValueType. 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: JsonFormatterTest.java    From jboss-logmanager-ext with Apache License 2.0 6 votes vote down vote up
private static Map<String, String> getMap(final JsonObject json, final Key key) {
    final String name = getKey(key);
    if (json.containsKey(name) && !json.isNull(name)) {
        final Map<String, String> result = new LinkedHashMap<>();
        final JsonObject mdcObject = json.getJsonObject(name);
        for (String k : mdcObject.keySet()) {
            final JsonValue value = mdcObject.get(k);
            if (value.getValueType() == ValueType.STRING) {
                result.put(k, value.toString().replace("\"", ""));
            } else {
                result.put(k, value.toString());
            }
        }
        return result;
    }
    return Collections.emptyMap();
}
 
Example #2
Source File: JsonpRuntime.java    From jmespath-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public List<JsonValue> toList(JsonValue value) {
  ValueType valueType = value.getValueType();
  if (valueType == ARRAY) {
    return new JsonArrayListWrapper((JsonArray) value);
  } else if (valueType == OBJECT) {
    JsonObject obj = (JsonObject) value;
    if (!obj.isEmpty()) {
      List<JsonValue> elements = new ArrayList<>(obj.size());
      for (JsonValue v : obj.values()) {
        elements.add(nodeOrNullNode(v));
      }
      return elements;
    }
  }
  return Collections.emptyList();
}
 
Example #3
Source File: JsonArrayReader.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private Object readItem(IndexedLocationBuilder locationBuilder, JsonValue itemValue) {
    Location itemLocation = locationBuilder.nextLocation();
    TypeInfo itemType = getItemType();
    if (itemValue.getValueType() == ValueType.NULL && itemType.isNonNull())
        throw new GraphQlClientException("invalid null " + itemLocation);
    return readJson(itemLocation, itemType, itemValue);
}
 
Example #4
Source File: JsonValueResolver.java    From trimou with Apache License 2.0 5 votes vote down vote up
@Override
public Object resolve(Object contextObject, String name,
        ResolutionContext context) {

    if (contextObject == null || !(contextObject instanceof JsonValue)) {
        return null;
    }

    JsonValue jsonValue = (JsonValue) contextObject;

    if (ValueType.ARRAY.equals(jsonValue.getValueType())
            && isAnIndex(name)) {
        // Index-based access of JsonArray elements
        JsonArray jsonArray = (JsonArray) jsonValue;
        // #26 Unwrap the element if necessary
        final Integer index = getIndexValue(name, context.getKey(),
                jsonArray.size());
        if (index != null) {
            return unwrapJsonValueIfNecessary(jsonArray.get(index));
        }
    } else if (ValueType.OBJECT.equals(jsonValue.getValueType())) {
        // JsonObject properties
        JsonObject jsonObject = (JsonObject) jsonValue;
        JsonValue member = jsonObject.get(name);
        if (member != null) {
            return unwrapJsonValueIfNecessary(member);
        }
    } else if (name.equals(NAME_UNWRAP_THIS)) {
        return unwrapJsonValueIfNecessary(jsonValue);
    }
    return null;
}
 
Example #5
Source File: JSONContentHandler.java    From jcypher with Apache License 2.0 5 votes vote down vote up
@Override
public PathInfo getPathInfo(String colKey) {
	PathInfo pathInfo = null;
	int colIdx = getColumnIndex(colKey);
	if (colIdx == -1)
		throw new RuntimeException("no result column: " + colKey);
	JsonObject dataObject = (JsonObject) this.jsonValue;
	JsonArray restArray = getRestArray(dataObject);
	JsonValue restValue = getRestValue(restArray, colIdx);
	restValue = this.handleArrayCase(restValue);
	
	if (restValue.getValueType() == ValueType.OBJECT) {
		JsonObject pathObject = (JsonObject) restValue;
		String str = pathObject.getString("start");
		long startId = Long.parseLong(str.substring(str.lastIndexOf('/') + 1));
		str = pathObject.getString("end");
		long endId = Long.parseLong(str.substring(str.lastIndexOf('/') + 1));
		JsonArray rels = pathObject.getJsonArray("relationships");
		List<Long> relIds = new ArrayList<Long>();
		int sz = rels.size();
		for (int i = 0; i < sz; i++) {
			String rel = rels.getString(i);
			long rid = Long.parseLong(rel.substring(rel.lastIndexOf('/') + 1));
			relIds.add(Long.valueOf(rid));
		}
		pathInfo = new PathInfo(startId, endId, relIds, pathObject);
	}
	return pathInfo;
}
 
Example #6
Source File: JSONContentHandler.java    From jcypher with Apache License 2.0 5 votes vote down vote up
@Override
public Object convertContentValue(Object value) {
	if (value instanceof JsonValue) {
		JsonValue val = (JsonValue) value;
		Object ret = null;
		ValueType typ = val.getValueType();
		if (typ == ValueType.NUMBER)
			ret = ((JsonNumber)val).bigDecimalValue();
		else if (typ == ValueType.STRING)
			ret = ((JsonString)val).getString();
		else if (typ == ValueType.FALSE)
			ret = Boolean.FALSE;
		else if (typ == ValueType.TRUE)
			ret = Boolean.TRUE;
		else if (typ == ValueType.ARRAY) {
			JsonArray arr = (JsonArray)val;
			List<Object> vals = new ArrayList<Object>();
			int sz = arr.size();
			for (int i = 0; i < sz; i++) {
				JsonValue v = arr.get(i);
				vals.add(convertContentValue(v));
			}
			ret = vals;
		} else if (typ == ValueType.OBJECT) {
			//JsonObject obj = (JsonObject)val;
		}
		return ret;
	}
	return value;
}
 
Example #7
Source File: BooleanCharacteristic.java    From HAP-Java with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
protected Boolean convert(JsonValue jsonValue) {
  if (jsonValue.getValueType().equals(ValueType.NUMBER)) {
    return ((JsonNumber) jsonValue).intValue() > 0;
  }
  return jsonValue.equals(JsonValue.TRUE);
}
 
Example #8
Source File: MonaJsonParser.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * read from META_DATA array
 * 
 * @param main
 * @param id
 * @return String or Number or null
 */
private Object readMetaData(JsonObject main, String id) {
  JsonValue j = main.getJsonArray(META_DATA).stream().map(v -> v.asJsonObject())
      .filter(v -> v.getString("name").equals(id)).map(v -> v.get("value")).findFirst()
      .orElse(null);

  if (j != null) {
    if (j.getValueType().equals(ValueType.STRING))
      return ((JsonString) j).getString();
    if (j.getValueType().equals(ValueType.NUMBER))
      return ((JsonNumber) j).numberValue();
  }
  return null;
}
 
Example #9
Source File: DataValueText.java    From tcases with MIT License 5 votes vote down vote up
/**
 * Returns the converted form of the given {@link DataValue}.
 */
public String convert( DataValue<?> value)
  {
  JsonValue jsonValue = convertJsonValue_.convert( value);
  ValueType type = Optional.ofNullable( jsonValue).map( JsonValue::getValueType).orElse( null);

  return
    type == ValueType.STRING
    ? ((JsonString) jsonValue).getString()
    : Objects.toString( jsonValue, "");
  }
 
Example #10
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
private static JsonObject getJsonObject(JsonObject object, String propName) {
    JsonObject obj = null;
    JsonValue val = object.get(propName);
    if (val != null && val.getValueType() == ValueType.OBJECT) {
        obj = val.asJsonObject();
    }
    return obj;
}
 
Example #11
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
private static Boolean getJsonValueAsBoolean(JsonValue value) {
    if (value != null) {
        if (value.getValueType() == ValueType.TRUE) {
            return true;
        } else if (value.getValueType() == ValueType.FALSE) {
            return false;
        }
    }
    return null;
}
 
Example #12
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 5 votes vote down vote up
private static List<JsonObject> getJsonValueAsList(JsonValue value) {
    if (value != null) {
        if (value.getValueType() == ValueType.ARRAY) {
            return value.asJsonArray().getValuesAs(JsonObject.class);

        } else if (value.getValueType() == ValueType.OBJECT) {
            List<JsonObject> ret = new ArrayList<>();
            ret.add(value.asJsonObject());

            return ret;
        }
    }
    return null;
}
 
Example #13
Source File: JsonUtil.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a JsonArray to a String array.
 * @param jsa the JsonArray
 * @return the String array
 */
public static String[] toStringArray(JsonArray jsa) {
  ArrayList<String> list = new ArrayList<String>();
  for (JsonValue v: jsa) {
    if ((v != null) && v.getValueType().equals(ValueType.STRING)) {
      list.add(((JsonString)v).getString());
    }
  }
  return list.toArray(new String[0]);
}
 
Example #14
Source File: JsonUtil.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new object builder.
 * @param json the json string
 * @return the builder
 */
public static JsonObjectBuilder newObjectBuilder(String json) {
  JsonObjectBuilder jb = Json.createObjectBuilder();
  if (json != null && json.length() > 0) {
    JsonStructure js = toJsonStructure(json);
    if (js != null && js.getValueType().equals(ValueType.OBJECT)) {
      jb = newObjectBuilder((JsonObject)js);
    }
  }
  return jb;
}
 
Example #15
Source File: JsonUtil.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a string value.
 * @param jso the json object
 * @param name the property name
 * @return the stgring value
 */
public static String getString(JsonObject jso, String name) {
  if (jso != null && jso.containsKey(name) && !jso.isNull(name)) {
    JsonValue v = jso.get(name);
    if (v.getValueType().equals(ValueType.STRING)) {
      return jso.getString(name);
    } else if (v.getValueType().equals(ValueType.NUMBER)) {
      // TODO Convert to string?
    } 
  }
  return null;
}
 
Example #16
Source File: WebHCatJsonParser.java    From hadoop-etl-udfs with MIT License 5 votes vote down vote up
public static List<HCatSerDeParameter> parseSerDeParameters(String json) throws Exception {
    assert(!(json == null) && !json.isEmpty());
    JsonObject obj = JsonUtil.getJsonObject(json);
    List<HCatSerDeParameter> serDeParameters = new ArrayList<>();
    for (Entry<String, JsonValue> param : obj.entrySet()) {
        assert(param.getValue().getValueType() == ValueType.STRING);
        serDeParameters.add(new HCatSerDeParameter(param.getKey(), ((JsonString)param.getValue()).getString()));
    }
    return serDeParameters;
}
 
Example #17
Source File: WebHCatJsonParser.java    From hadoop-etl-udfs with MIT License 5 votes vote down vote up
public static List<HCatSerDeParameter> parseSerDeParameters(JsonObject obj) {
    List<HCatSerDeParameter> serDeParameters = new ArrayList<>();
    for (Entry<String, JsonValue> serdeParam : obj.entrySet()) {
        assert(serdeParam.getValue().getValueType() == ValueType.STRING);
        serDeParameters.add(new HCatSerDeParameter(serdeParam.getKey(), ((JsonString)serdeParam.getValue()).getString()));
    }
    return serDeParameters;
}
 
Example #18
Source File: JsonFileArgumentsProvider.java    From junit-json-params with Apache License 2.0 5 votes vote down vote up
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
    boolean isList = stream(context.getRequiredTestMethod().getParameterTypes())
        .anyMatch(List.class::isAssignableFrom);
    return stream(resources)
            .map(resource -> openInputStream(context, resource))
            .map(JsonFileArgumentsProvider::values)
            .flatMap(json -> {
                if(json.getValueType() == ValueType.ARRAY && !isList){
                    return json.asJsonArray().stream();
                }
                return Stream.of(json);
            })
        .map(Arguments::arguments);
}
 
Example #19
Source File: MonaJsonParser.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * read from META_DATA array
 * 
 * @param main
 * @param id
 * @return String or Number or null
 */
private Object readMetaData(JsonObject main, String id) {
  JsonValue j = main.getJsonArray(META_DATA).stream().map(v -> v.asJsonObject())
      .filter(v -> v.getString("name").equals(id)).map(v -> v.get("value")).findFirst()
      .orElse(null);

  if (j != null) {
    if (j.getValueType().equals(ValueType.STRING))
      return ((JsonString) j).getString();
    if (j.getValueType().equals(ValueType.NUMBER))
      return ((JsonNumber) j).numberValue();
  }
  return null;
}
 
Example #20
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static String getJsonValueAsString(JsonValue value) {
    return (value != null && value.getValueType() == ValueType.STRING) ? ((JsonString) value).getString() : null;
}
 
Example #21
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 #22
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static JsonArray getJsonValueAsArray(JsonValue value) {
    return (value != null && value.getValueType() == ValueType.ARRAY) ? value.asJsonArray() : null;
}
 
Example #23
Source File: NetworkConfig.java    From fabric-sdk-java with Apache License 2.0 4 votes vote down vote up
private static JsonObject getJsonValueAsObject(JsonValue value) {
    return (value != null && value.getValueType() == ValueType.OBJECT) ? value.asJsonObject() : null;
}
 
Example #24
Source File: RequestCaseJson.java    From tcases with MIT License 4 votes vote down vote up
/**
 * Returns the DataValue represented by the given JSON object.
 */
private static DataValue<?> asDataValue( RequestCaseContext context, JsonValue json)
  {
  DataValue<?> data;
  ValueType jsonType = json.getValueType();
  if( jsonType == ValueType.NULL)
    {
    data = null;
    }
  else if( jsonType == ValueType.OBJECT)
    {
    JsonObject object = (JsonObject) json;
    String type = context.resultFor( TYPE, () -> object.getString( TYPE));
    String format = context.resultFor( FORMAT, () -> object.getString( FORMAT, null));

    data =
      context.resultFor( VALUE, () -> {
        return
          type.equals( "array")?
          asArrayValue( context, object.getJsonArray( VALUE)) :

          type.equals( "boolean")?
          new BooleanValue( object.getBoolean( VALUE)) :
      
          type.equals( "integer")?
          asIntegerValue( context, object.getJsonNumber( VALUE), format) :

          type.equals( "null")?
          new NullValue() :

          type.equals( "number")?
          new DecimalValue( object.getJsonNumber( VALUE).bigDecimalValue(), format) :
      
          type.equals( "object")?
          asObjectValue( context, object.getJsonObject( VALUE)) :

          asStringValue( context, object.getString( VALUE), format);
        });
    }
  else
    {
    throw new RequestCaseException( String.format( "Invalid value type=%s -- must be either \"null\" or \"object\"", jsonType));
    }
  
  return data;
  }
 
Example #25
Source File: EcommerceItemTest.java    From matomo-java-tracker with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test of getValueType method, of class EcommerceItem.
 */
@Test
public void testGetValueType(){
    assertEquals(ValueType.ARRAY, ecommerceItem.getValueType());
}
 
Example #26
Source File: RecordJsonGenerator.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public JsonGenerator write(final String name, final JsonValue value) {
    switch (value.getValueType()) {
    case ARRAY:
        JsonValue jv = JsonValue.class.cast(Collection.class.cast(value).iterator().next());
        if (jv.getValueType().equals(ValueType.TRUE) || jv.getValueType().equals(ValueType.FALSE)) {
            objectBuilder
                    .withArray(
                            factory
                                    .newEntryBuilder()
                                    .withName(name)
                                    .withType(Type.ARRAY)
                                    .withElementSchema(factory.newSchemaBuilder(Type.BOOLEAN).build())
                                    .build(),
                            Collection.class
                                    .cast(Collection.class
                                            .cast(value)
                                            .stream()
                                            .map(v -> JsonValue.class.cast(v).getValueType().equals(ValueType.TRUE))
                                            .collect(toList())));
        } else {
            objectBuilder
                    .withArray(createEntryForJsonArray(name, Collection.class.cast(value)),
                            Collection.class.cast(value));
        }
        break;
    case OBJECT:
        Record r = recordConverters.toRecord(mappingRegistry, value, () -> jsonb, () -> factory);
        objectBuilder.withRecord(name, r);
        break;
    case STRING:
        objectBuilder.withString(name, JsonString.class.cast(value).getString());
        break;
    case NUMBER:
        objectBuilder.withDouble(name, JsonNumber.class.cast(value).numberValue().doubleValue());
        break;
    case TRUE:
        objectBuilder.withBoolean(name, true);
        break;
    case FALSE:
        objectBuilder.withBoolean(name, false);
        break;
    case NULL:
        break;
    default:
        throw new IllegalStateException("Unexpected value: " + value.getValueType());
    }
    return this;
}
 
Example #27
Source File: JSONContentHandler.java    From jcypher with Apache License 2.0 4 votes vote down vote up
private JsonValue handleArrayCase(JsonValue obj) {
	if (obj.getValueType() == ValueType.ARRAY && ((JsonArray)obj).size() > 0)
		return ((JsonArray)obj).get(0);
	return obj;
}