本文整理汇总了Java中javax.json.JsonNumber类的典型用法代码示例。如果您正苦于以下问题:Java JsonNumber类的具体用法?Java JsonNumber怎么用?Java JsonNumber使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonNumber类属于javax.json包,在下文中一共展示了JsonNumber类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: verifyInjectedCustomDouble
import javax.json.JsonNumber; //导入依赖的package包/类
@GET
@Path("/verifyInjectedCustomDouble")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomDouble(@QueryParam("value") Double value) {
boolean pass = false;
String msg;
// iat
JsonNumber customValue = customDouble.get();
if(customValue == null) {
msg = "customDouble value is null, FAIL";
}
else if(Math.abs(customValue.doubleValue() - value.doubleValue()) < 0.00001) {
msg = "customDouble PASS";
pass = true;
}
else {
msg = String.format("customDouble: %s != %.8f", customValue, value);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
示例2: verifyInjectedCustomDouble
import javax.json.JsonNumber; //导入依赖的package包/类
@GET
@Path("/verifyInjectedCustomDouble")
@Produces(MediaType.APPLICATION_JSON)
public JsonObject verifyInjectedCustomDouble(@QueryParam("value") Double value) {
boolean pass = false;
String msg;
// iat
JsonNumber customValue = customDouble.getValue();
if(customValue == null) {
msg = "customDouble value is null, FAIL";
}
else if(Math.abs(customValue.doubleValue() - value.doubleValue()) < 0.00001) {
msg = "customDouble PASS";
pass = true;
}
else {
msg = String.format("customDouble: %s != %.8f", customValue, value);
}
JsonObject result = Json.createObjectBuilder()
.add("pass", pass)
.add("msg", msg)
.build();
return result;
}
示例3: toJavaObject
import javax.json.JsonNumber; //导入依赖的package包/类
/**
* Converts a {@link JsonValue} to its corresponding Java object. Values of type {@link
* JsonObject} or {@link JsonArray} are converted as specified by {@link #toJavaMap} and {@link
* #toJavaList}, respectively.
*/
@Nullable
public static Object toJavaObject(JsonValue value) {
switch (value.getValueType()) {
case ARRAY:
return toJavaList((JsonArray) value);
case FALSE:
return Boolean.FALSE;
case NULL:
return null;
case NUMBER:
JsonNumber number = (JsonNumber) value;
return number.isIntegral() ? number.intValue() : number.doubleValue();
case OBJECT:
return toJavaMap((JsonObject) value);
case STRING:
return ((JsonString) value).getString();
case TRUE:
return Boolean.TRUE;
default:
throw new VerifyException("Json value with unknown type: " + value);
}
}
示例4: unbox
import javax.json.JsonNumber; //导入依赖的package包/类
public static Object unbox(JsonValue value, Function<JsonStructure, Object> convert) throws JsonException {
switch (value.getValueType()) {
case ARRAY:
case OBJECT:
return convert.apply((JsonStructure) value);
case FALSE:
return Boolean.FALSE;
case TRUE:
return Boolean.TRUE;
case NULL:
return null;
case NUMBER:
JsonNumber number = (JsonNumber) value;
return number.isIntegral() ? number.longValue() : number.doubleValue();
case STRING:
return ((JsonString) value).getString();
default:
throw new JsonException("Unknow value type");
}
}
示例5: testInt
import javax.json.JsonNumber; //导入依赖的package包/类
@Test
public void testInt ()
{
JsonNumber v = new CookJsonInt (1234);
Assert.assertEquals (ValueType.NUMBER, v.getValueType ());
Assert.assertEquals (true, v.isIntegral ());
Assert.assertEquals (1234, v.intValue ());
Assert.assertEquals (1234, v.intValueExact ());
Assert.assertEquals (1234, v.longValue ());
Assert.assertEquals (1234, v.longValueExact ());
Assert.assertEquals (new BigInteger ("1234"), v.bigIntegerValue ());
Assert.assertEquals (new BigInteger ("1234"), v.bigIntegerValueExact ());
Assert.assertEquals (new BigDecimal (1234), v.bigDecimalValue ());
Assert.assertEquals (1234, v.doubleValue (), 0);
Assert.assertEquals (new BigDecimal (1234).hashCode (), v.hashCode ());
Assert.assertEquals ("1234", v.toString ());
}
示例6: fromJSON
import javax.json.JsonNumber; //导入依赖的package包/类
public static SessionRating fromJSON(JsonObject sessionRatingJson) {
String id = getStringFromJson("id", sessionRatingJson);
String session = getStringFromJson("session", sessionRatingJson);
String attendeeId = getStringFromJson("attendeeId", sessionRatingJson);
String _rev = getStringFromJson("_rev", sessionRatingJson);
String _id = getStringFromJson("_id", sessionRatingJson);
int rating = 0;
if (sessionRatingJson.containsKey("rating")) {
JsonNumber ratingJson = sessionRatingJson.getJsonNumber("rating");
if (ratingJson != null) {
rating = ratingJson.intValue();
}
}
SessionRating sessionRating = new SessionRating(id, session, attendeeId, rating, _id, _rev);
return sessionRating;
}
示例7: testDouble
import javax.json.JsonNumber; //导入依赖的package包/类
@Test
public void testDouble ()
{
double d = 1234;
JsonNumber v = new CookJsonDouble (d);
Assert.assertEquals (ValueType.NUMBER, v.getValueType ());
Assert.assertEquals (false, v.isIntegral ());
Assert.assertEquals (1234, v.intValue ());
Assert.assertEquals (1234, v.intValueExact ());
Assert.assertEquals (1234, v.longValue ());
Assert.assertEquals (1234, v.longValueExact ());
Assert.assertEquals (new BigInteger ("1234"), v.bigIntegerValue ());
Assert.assertEquals (new BigInteger ("1234"), v.bigIntegerValueExact ());
Assert.assertEquals (BigDecimal.valueOf (d), v.bigDecimalValue ());
Assert.assertEquals (d, v.doubleValue (), 0);
Assert.assertEquals (BigDecimal.valueOf (d).hashCode (), v.hashCode ());
Assert.assertEquals (DoubleUtils.toString (d), v.toString ());
}
示例8: testBigInteger
import javax.json.JsonNumber; //导入依赖的package包/类
@Test
public void testBigInteger ()
{
BigInteger d = new BigInteger ("1234");
JsonNumber v = new CookJsonBigDecimal (d);
Assert.assertEquals (ValueType.NUMBER, v.getValueType ());
Assert.assertEquals (true, v.isIntegral ());
Assert.assertEquals (d.intValue (), v.intValue ());
Assert.assertEquals (d.intValue (), v.intValueExact ());
Assert.assertEquals (d.longValue (), v.longValue ());
Assert.assertEquals (d.longValue (), v.longValueExact ());
Assert.assertEquals (d, v.bigIntegerValue ());
Assert.assertEquals (d, v.bigIntegerValueExact ());
Assert.assertEquals (new BigDecimal (d), v.bigDecimalValue ());
Assert.assertEquals (d.doubleValue (), v.doubleValue (), 0);
Assert.assertEquals (new BigDecimal (d).hashCode (), v.hashCode ());
Assert.assertEquals (d.toString (), v.toString ());
}
示例9: plainifyJsonValue
import javax.json.JsonNumber; //导入依赖的package包/类
private Object plainifyJsonValue(final JsonValue jval) {
switch (jval.getValueType()) {
case ARRAY:
return plainifyJsonArray((JsonArray) jval);
case FALSE:
return Boolean.FALSE;
case TRUE:
return Boolean.TRUE;
case NULL:
return null;
case NUMBER:
return ((JsonNumber) jval).bigDecimalValue();
case OBJECT:
return plainifyJsonObject((JsonObject) jval);
case STRING:
return ((JsonString) jval).getString();
default:
throw new RuntimeException("unexpected json type");
}
}
示例10: getBoolean
import javax.json.JsonNumber; //导入依赖的package包/类
public static Boolean getBoolean( JsonObject o, String n, Boolean defaultValue )
{
JsonValue v = o.get( n );
if( v == null ) {
return defaultValue;
}
switch( v.getValueType() ) {
case NUMBER:
JsonNumber jn = (JsonNumber) v;
return !jn.bigIntegerValue().
equals( BigInteger.ZERO );
case STRING:
JsonString s = (JsonString) v;
return Boolean.parseBoolean( s.getString() );
case TRUE:
return true;
case FALSE:
return false;
case NULL:
return defaultValue;
default:
return Boolean.parseBoolean( v.toString() );
}
}
示例11: manyDuplications
import javax.json.JsonNumber; //导入依赖的package包/类
@Test
public void manyDuplications() {
String json = "[123, 456, 456, 123]";
Schema schema = schema(array(integer()).unique());
JsonValidator validator = new BasicJsonValidator(schema);
ValidationResult result = validator.validate(new StringReader(json));
assertResultValid(result, json);
assertThat(result.getProblems().size(), equalTo(2));
assertThat(result.getProblems().get(0), instanceOf(ArrayDuplicateItemProblem.class));
ArrayDuplicateItemProblem p0 = (ArrayDuplicateItemProblem)result.getProblems().get(0);
assertThat(p0.getPointer().toString(), equalTo(""));
assertThat(p0.getDuplicateIndex(), equalTo(2));
assertThat(((JsonNumber)p0.getDuplicateItem()).intValue(), equalTo(456));
assertNotNull(p0.getDescription());
assertThat(result.getProblems().get(1), instanceOf(ArrayDuplicateItemProblem.class));
ArrayDuplicateItemProblem p1 = (ArrayDuplicateItemProblem)result.getProblems().get(1);
assertThat(p1.getPointer().toString(), equalTo(""));
assertThat(p1.getDuplicateIndex(), equalTo(3));
assertThat(((JsonNumber)p1.getDuplicateItem()).intValue(), equalTo(123));
assertNotNull(p1.getDescription());
}
示例12: notExistInNone
import javax.json.JsonNumber; //导入依赖的package包/类
@Test
public void notExistInNone() {
String json = "[12.34]";
Schema schema = schema(array(number().enumeration()));
JsonValidator validator = new BasicJsonValidator(schema);
ValidationResult result = validator.validate(new StringReader(json));
assertResultValid(result, json);
assertEquals(1, result.getProblems().size());
assertTrue(result.getProblems().get(0) instanceof NoSuchEnumeratorProblem);
NoSuchEnumeratorProblem p = (NoSuchEnumeratorProblem)result.getProblems().get(0);
assertEquals(new BigDecimal("12.34"), ((JsonNumber)p.getCauseValue()).bigDecimalValue());
Set<Object> expected = p.getEnumerators();
assertEquals(0, expected.size());
assertNotNull(p.getDescription());
}
示例13: getLocalTime
import javax.json.JsonNumber; //导入依赖的package包/类
public static LocalTime getLocalTime( JsonObject o, String n )
{
JsonValue v = o.get( n );
if( v == null ) {
return null;
}
switch( v.getValueType() ) {
case STRING:
return TimeUtils.getLocalTime( ((JsonString) v).getString() );
case NUMBER:
return TimeUtils.getLocalTime( ((JsonNumber) v).longValue() );
default:
return null;
}
}
示例14: notExistInMany
import javax.json.JsonNumber; //导入依赖的package包/类
@Test
public void notExistInMany() {
String json = "[3.14]";
Schema schema = schema(array(number().enumeration(new BigDecimal("12.34"), new BigDecimal("56.78"))));
JsonValidator validator = new BasicJsonValidator(schema);
ValidationResult result = validator.validate(new StringReader(json));
assertResultValid(result, json);
assertEquals(1, result.getProblems().size());
assertTrue(result.getProblems().get(0) instanceof NoSuchEnumeratorProblem);
NoSuchEnumeratorProblem p = (NoSuchEnumeratorProblem)result.getProblems().get(0);
assertEquals(new BigDecimal("3.14"), ((JsonNumber)p.getCauseValue()).bigDecimalValue());
Set<Object> expected = p.getEnumerators();
assertEquals(2, expected.size());
assertNotNull(p.getDescription());
}
示例15: notExistInNone
import javax.json.JsonNumber; //导入依赖的package包/类
@Test
public void notExistInNone() {
String json = "[1]";
Schema schema = schema(array(integer().enumeration()));
JsonValidator validator = new BasicJsonValidator(schema);
ValidationResult result = validator.validate(new StringReader(json));
assertResultValid(result, json);
assertEquals(1, result.getProblems().size());
assertTrue(result.getProblems().get(0) instanceof NoSuchEnumeratorProblem);
NoSuchEnumeratorProblem p = (NoSuchEnumeratorProblem)result.getProblems().get(0);
assertEquals(1, ((JsonNumber)p.getCauseValue()).intValue());
Set<Object> expected = p.getEnumerators();
assertEquals(0, expected.size());
assertNotNull(p.getDescription());
}