com.google.gson.internal.LazilyParsedNumber Java Examples

The following examples show how to use com.google.gson.internal.LazilyParsedNumber. 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 Project: MyTown2   Author: MyEssentials   File: Segment.java    License: The Unlicense 6 votes vote down vote up
protected int getRange(Object object) {
    if (!getters.contains("range")) {
        return 0;
    }
    try {
        Object rangeObj = null;
        rangeObj = getters.get("range").invoke(Object.class, object, object);
        if (rangeObj instanceof LazilyParsedNumber) {
            return ((LazilyParsedNumber)rangeObj).intValue();
        } else if (rangeObj instanceof Double) {
            return (int)((Double)rangeObj + 0.5);
        } else if (rangeObj instanceof Integer) {
            return (Integer)rangeObj;
        }
    } catch (GetterException ex) {
    }
    return 0;
}
 
Example #2
Source Project: react-native-sip   Author: florindumitru   File: ArgumentUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static WritableMap fromJsonObject(JsonObject object) {
    WritableMap result = new WritableNativeMap();

    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        Object value = fromJson(entry.getValue());

        if (value instanceof WritableMap) {
            result.putMap(entry.getKey(), (WritableMap) value);
        } else if (value instanceof WritableArray) {
            result.putArray(entry.getKey(), (WritableArray) value);
        } else if (value instanceof String) {
            result.putString(entry.getKey(), (String) value);
        } else if (value instanceof LazilyParsedNumber) {
            result.putInt(entry.getKey(), ((LazilyParsedNumber) value).intValue());
        } else if (value instanceof Integer) {
            result.putInt(entry.getKey(), (Integer) value);
        } else if (value instanceof Double) {
            result.putDouble(entry.getKey(), (Double) value);
        } else if (value instanceof Boolean) {
            result.putBoolean(entry.getKey(), (Boolean) value);
        } else {
            Log.d("ArgumentUtils", "Unknown type: " + value.getClass().getName());
            result.putNull(entry.getKey());
        }
    }

    return result;
}
 
Example #3
Source Project: react-native-sip   Author: florindumitru   File: ArgumentUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static WritableArray fromJsonArray(JsonArray arr) {
    WritableArray result = new WritableNativeArray();

    for (JsonElement el : arr) {
        Object item = fromJson(el);

        if (item instanceof WritableMap) {
            result.pushMap((WritableMap) item);
        } else if (item instanceof WritableArray) {
            result.pushArray((WritableArray) item);
        } else if (item instanceof String) {
            result.pushString((String) item);
        } else if (item instanceof LazilyParsedNumber) {
            result.pushInt(((LazilyParsedNumber) item).intValue());
        } else if (item instanceof Integer) {
            result.pushInt((Integer) item);
        } else if (item instanceof Double) {
            result.pushDouble((Double) item);
        } else if (item instanceof Boolean) {
            result.pushBoolean((Boolean) item);
        } else {

            Log.d("ArgumentUtils", "Unknown type: " + item.getClass().getName());

            result.pushNull();
        }
    }

    return result;
}
 
Example #4
Source Project: letv   Author: JackChan1999   File: TypeAdapters.java    License: Apache License 2.0 5 votes vote down vote up
public JsonElement read(JsonReader in) throws IOException {
    switch (in.peek()) {
        case NUMBER:
            return new JsonPrimitive(new LazilyParsedNumber(in.nextString()));
        case BOOLEAN:
            return new JsonPrimitive(Boolean.valueOf(in.nextBoolean()));
        case STRING:
            return new JsonPrimitive(in.nextString());
        case NULL:
            in.nextNull();
            return JsonNull.INSTANCE;
        case BEGIN_ARRAY:
            JsonElement array = new JsonArray();
            in.beginArray();
            while (in.hasNext()) {
                array.add(read(in));
            }
            in.endArray();
            return array;
        case BEGIN_OBJECT:
            JsonElement object = new JsonObject();
            in.beginObject();
            while (in.hasNext()) {
                object.add(in.nextName(), read(in));
            }
            in.endObject();
            return object;
        default:
            throw new IllegalArgumentException();
    }
}
 
Example #5
Source Project: letv   Author: JackChan1999   File: TypeAdapters.java    License: Apache License 2.0 5 votes vote down vote up
public Number read(JsonReader in) throws IOException {
    JsonToken jsonToken = in.peek();
    switch (jsonToken) {
        case NUMBER:
            return new LazilyParsedNumber(in.nextString());
        case NULL:
            in.nextNull();
            return null;
        default:
            throw new JsonSyntaxException("Expecting number, got: " + jsonToken);
    }
}
 
Example #6
Source Project: react-native-pjsip   Author: datso   File: ArgumentUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static WritableMap fromJsonObject(JsonObject object) {
    WritableMap result = new WritableNativeMap();

    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
        Object value = fromJson(entry.getValue());

        if (value instanceof WritableMap) {
            result.putMap(entry.getKey(), (WritableMap) value);
        } else if (value instanceof WritableArray) {
            result.putArray(entry.getKey(), (WritableArray) value);
        } else if (value instanceof String) {
            result.putString(entry.getKey(), (String) value);
        } else if (value instanceof LazilyParsedNumber) {
            result.putInt(entry.getKey(), ((LazilyParsedNumber) value).intValue());
        } else if (value instanceof Integer) {
            result.putInt(entry.getKey(), (Integer) value);
        } else if (value instanceof Double) {
            result.putDouble(entry.getKey(), (Double) value);
        } else if (value instanceof Boolean) {
            result.putBoolean(entry.getKey(), (Boolean) value);
        } else {
            Log.d("ArgumentUtils", "Unknown type: " + value.getClass().getName());
            result.putNull(entry.getKey());
        }
    }

    return result;
}
 
Example #7
Source Project: react-native-pjsip   Author: datso   File: ArgumentUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static WritableArray fromJsonArray(JsonArray arr) {
    WritableArray result = new WritableNativeArray();

    for (JsonElement el : arr) {
        Object item = fromJson(el);

        if (item instanceof WritableMap) {
            result.pushMap((WritableMap) item);
        } else if (item instanceof WritableArray) {
            result.pushArray((WritableArray) item);
        } else if (item instanceof String) {
            result.pushString((String) item);
        } else if (item instanceof LazilyParsedNumber) {
            result.pushInt(((LazilyParsedNumber) item).intValue());
        } else if (item instanceof Integer) {
            result.pushInt((Integer) item);
        } else if (item instanceof Double) {
            result.pushDouble((Double) item);
        } else if (item instanceof Boolean) {
            result.pushBoolean((Boolean) item);
        } else {

            Log.d("ArgumentUtils", "Unknown type: " + item.getClass().getName());

            result.pushNull();
        }
    }

    return result;
}
 
Example #8
Source Project: external-resources   Author: prcaen   File: Resource.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable protected Number getAsNumber() {
  if (isString()) {
    return new LazilyParsedNumber((String) value);
  } else if (isNumber()) {
    return (Number) value;
  } else {
    return null;
  }
}
 
Example #9
Source Project: java-sdk   Author: watson-developer-cloud   File: DiscoveryServiceIT.java    License: Apache License 2.0 5 votes vote down vote up
/** Query with sort is successful. */
@Test
public void queryWithSortIsSuccessful() {
  QueryOptions.Builder queryBuilder = new QueryOptions.Builder(environmentId, collectionId);
  String sortList = "field";
  queryBuilder.sort(sortList);
  QueryResponse queryResponse = discovery.query(queryBuilder.build()).execute().getResult();
  assertTrue(queryResponse.getResults().size() > 1);
  int v0 = ((LazilyParsedNumber) queryResponse.getResults().get(0).get("field")).intValue();
  int v1 = ((LazilyParsedNumber) queryResponse.getResults().get(1).get("field")).intValue();
  assertTrue(v0 <= v1);
}
 
Example #10
Source Project: MiBandDecompiled   Author: vishnudevk   File: JsonPrimitive.java    License: Apache License 2.0 5 votes vote down vote up
public Number getAsNumber()
{
    if (b instanceof String)
    {
        return new LazilyParsedNumber((String)b);
    } else
    {
        return (Number)b;
    }
}
 
Example #11
Source Project: gson   Author: google   File: TypeAdapters.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Number read(JsonReader in) throws IOException {
  JsonToken jsonToken = in.peek();
  switch (jsonToken) {
  case NULL:
    in.nextNull();
    return null;
  case NUMBER:
  case STRING:
    return new LazilyParsedNumber(in.nextString());
  default:
    throw new JsonSyntaxException("Expecting number, got: " + jsonToken);
  }
}
 
Example #12
Source Project: gson   Author: google   File: TypeAdapters.java    License: Apache License 2.0 5 votes vote down vote up
@Override public JsonElement read(JsonReader in) throws IOException {
  switch (in.peek()) {
  case STRING:
    return new JsonPrimitive(in.nextString());
  case NUMBER:
    String number = in.nextString();
    return new JsonPrimitive(new LazilyParsedNumber(number));
  case BOOLEAN:
    return new JsonPrimitive(in.nextBoolean());
  case NULL:
    in.nextNull();
    return JsonNull.INSTANCE;
  case BEGIN_ARRAY:
    JsonArray array = new JsonArray();
    in.beginArray();
    while (in.hasNext()) {
      array.add(read(in));
    }
    in.endArray();
    return array;
  case BEGIN_OBJECT:
    JsonObject object = new JsonObject();
    in.beginObject();
    while (in.hasNext()) {
      object.add(in.nextName(), read(in));
    }
    in.endObject();
    return object;
  case END_DOCUMENT:
  case NAME:
  case END_OBJECT:
  case END_ARRAY:
  default:
    throw new IllegalArgumentException();
  }
}
 
Example #13
Source Project: activitystreams   Author: OpenSocial   File: Adapters.java    License: Apache License 2.0 5 votes vote down vote up
private Object des(JsonElement val, JsonDeserializationContext context) {
  if (val.isJsonArray())
    return MultimapAdapter.arraydes(val.getAsJsonArray(), context);
  else if (val.isJsonObject())
    return context.deserialize(val, ASObject.class);
  else if (val.isJsonPrimitive()) {
    Object v = primConverter.convert(val.getAsJsonPrimitive());
    if (v instanceof LazilyParsedNumber) 
      v = new LazilyParsedNumberComparable((LazilyParsedNumber) v);
    return v;
  }
  else
    return null;
}
 
Example #14
Source Project: MyTown2   Author: MyEssentials   File: GetterConstant.java    License: The Unlicense 5 votes vote down vote up
@Override
public Object invoke(Class<?> returnType, Object instance, Object... parameters) {
    if (returnType.equals(Integer.class)) {
        return (Object)((LazilyParsedNumber)constant).intValue();
    }
    return constant;
}
 
Example #15
Source Project: framework   Author: Odoo-mobile   File: TypeAdapters.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Number read(JsonReader in) throws IOException {
  JsonToken jsonToken = in.peek();
  switch (jsonToken) {
  case NULL:
    in.nextNull();
    return null;
  case NUMBER:
    return new LazilyParsedNumber(in.nextString());
  default:
    throw new JsonSyntaxException("Expecting number, got: " + jsonToken);
  }
}
 
Example #16
Source Project: framework   Author: Odoo-mobile   File: TypeAdapters.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public JsonElement read(JsonReader in) throws IOException {
  switch (in.peek()) {
  case STRING:
    return new JsonPrimitive(in.nextString());
  case NUMBER:
    String number = in.nextString();
    return new JsonPrimitive(new LazilyParsedNumber(number));
  case BOOLEAN:
    return new JsonPrimitive(in.nextBoolean());
  case NULL:
    in.nextNull();
    return JsonNull.INSTANCE;
  case BEGIN_ARRAY:
    JsonArray array = new JsonArray();
    in.beginArray();
    while (in.hasNext()) {
      array.add(read(in));
    }
    in.endArray();
    return array;
  case BEGIN_OBJECT:
    JsonObject object = new JsonObject();
    in.beginObject();
    while (in.hasNext()) {
      object.add(in.nextName(), read(in));
    }
    in.endObject();
    return object;
  case END_DOCUMENT:
  case NAME:
  case END_OBJECT:
  case END_ARRAY:
  default:
    throw new IllegalArgumentException();
  }
}
 
Example #17
Source Project: blueflood   Author: rackerlabs   File: PreaggregateConversions.java    License: Apache License 2.0 5 votes vote down vote up
public static Number resolveNumber(Number n) {
    if (n instanceof LazilyParsedNumber) {
        try {
            return n.longValue();
        } catch (NumberFormatException ex) {
            return n.doubleValue();
        }
    } else {
        // already resolved.
        return n;
    }
}
 
Example #18
Source Project: blueflood   Author: rackerlabs   File: HttpAggregatedIngestionHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGsonNumberConversions() {
    AggregatedPayload payload = AggregatedPayload.create(payloadJson);
    Number doubleNum = new LazilyParsedNumber("2.321");
    assertEquals( Double.parseDouble( "2.321" ), PreaggregateConversions.resolveNumber( doubleNum ) );
    
    Number longNum = new LazilyParsedNumber("12345");
    assertEquals(Long.parseLong("12345"), PreaggregateConversions.resolveNumber(longNum));
}
 
Example #19
Source Project: letv   Author: JackChan1999   File: JsonPrimitive.java    License: Apache License 2.0 4 votes vote down vote up
public Number getAsNumber() {
    return this.value instanceof String ? new LazilyParsedNumber((String) this.value) : (Number) this.value;
}
 
Example #20
Source Project: activitystreams   Author: OpenSocial   File: GsonWrapper.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Method initGsonBuilder.
 * @param builder Builder

 * @return GsonBuilder */
private static GsonBuilder initGsonBuilder(
  Builder builder, 
  Schema schema, 
  ASObjectAdapter base,
  Iterable<AdapterEntry<?>> adapters) {
  
  GsonBuilder gson = new GsonBuilder()
  .registerTypeHierarchyAdapter(TypeValue.class, new TypeValueAdapter(schema))
  .registerTypeHierarchyAdapter(LinkValue.class, new LinkValueAdapter(schema))
  .registerTypeHierarchyAdapter(Iterable.class, ITERABLE)
  .registerTypeHierarchyAdapter(ASObject.class, base)
  .registerTypeHierarchyAdapter(Collection.class, base)
  .registerTypeHierarchyAdapter(Activity.class, base)
  .registerTypeHierarchyAdapter(NLV.class, NLV)
  .registerTypeHierarchyAdapter(ActionsValue.class, ACTIONS)
  .registerTypeHierarchyAdapter(Optional.class, OPTIONAL)
  .registerTypeHierarchyAdapter(Range.class, RANGE)
  .registerTypeHierarchyAdapter(Table.class, TABLE)
  .registerTypeHierarchyAdapter(LazilyParsedNumber.class, NUMBER)
  .registerTypeHierarchyAdapter(LazilyParsedNumberComparable.class, NUMBER)
  .registerTypeHierarchyAdapter(ReadableDuration.class, DURATION)
  .registerTypeHierarchyAdapter(ReadablePeriod.class, PERIOD)
  .registerTypeHierarchyAdapter(ReadableInterval.class, INTERVAL)
  .registerTypeAdapter(
    Activity.Status.class, 
    forEnum(
      Activity.Status.class, 
      Activity.Status.OTHER))
  .registerTypeAdapter(Date.class, DATE)
  .registerTypeAdapter(DateTime.class, DATETIME)
  .registerTypeAdapter(MediaType.class, MIMETYPE)
  .registerTypeHierarchyAdapter(Multimap.class, MULTIMAP);
  
  for (AdapterEntry<?> entry : adapters) {
    if (entry.hier)
      gson.registerTypeHierarchyAdapter(
        entry.type, 
        entry.adapter!=null ?
          entry.adapter : base);
    else
      gson.registerTypeAdapter(
        entry.type, 
        entry.adapter!=null ? 
          entry.adapter:base);
  }
  
  return gson;

}
 
Example #21
Source Project: proteus   Author: flipkart-incubator   File: ProteusTypeAdapterFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Value read(JsonReader in) throws IOException {
  switch (in.peek()) {
    case STRING:
      return compileString(getContext(), in.nextString());
    case NUMBER:
      String number = in.nextString();
      return new Primitive(new LazilyParsedNumber(number));
    case BOOLEAN:
      return new Primitive(in.nextBoolean());
    case NULL:
      in.nextNull();
      return Null.INSTANCE;
    case BEGIN_ARRAY:
      Array array = new Array();
      in.beginArray();
      while (in.hasNext()) {
        array.add(read(in));
      }
      in.endArray();
      return array;
    case BEGIN_OBJECT:
      ObjectValue object = new ObjectValue();
      in.beginObject();
      if (in.hasNext()) {
        String name = in.nextName();
        if (ProteusConstants.TYPE.equals(name) && JsonToken.STRING.equals(in.peek())) {
          String type = in.nextString();
          if (PROTEUS_INSTANCE_HOLDER.isLayout(type)) {
            Layout layout = LAYOUT_TYPE_ADAPTER.read(type, PROTEUS_INSTANCE_HOLDER.getProteus(), in);
            in.endObject();
            return layout;
          } else {
            object.add(name, compileString(getContext(), type));
          }
        } else {
          object.add(name, read(in));
        }
      }
      while (in.hasNext()) {
        object.add(in.nextName(), read(in));
      }
      in.endObject();
      return object;
    case END_DOCUMENT:
    case NAME:
    case END_OBJECT:
    case END_ARRAY:
    default:
      throw new IllegalArgumentException();
  }
}
 
Example #22
Source Project: proteus   Author: flipkart-incubator   File: ProteusTypeAdapterFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Value read(JsonReader in) throws IOException {
  switch (in.peek()) {
    case STRING:
      return compileString(getContext(), in.nextString());
    case NUMBER:
      String number = in.nextString();
      return new Primitive(new LazilyParsedNumber(number));
    case BOOLEAN:
      return new Primitive(in.nextBoolean());
    case NULL:
      in.nextNull();
      return Null.INSTANCE;
    case BEGIN_ARRAY:
      Array array = new Array();
      in.beginArray();
      while (in.hasNext()) {
        array.add(read(in));
      }
      in.endArray();
      return array;
    case BEGIN_OBJECT:
      ObjectValue object = new ObjectValue();
      in.beginObject();
      if (in.hasNext()) {
        String name = in.nextName();
        if (TYPE.equals(name) && JsonToken.NUMBER.equals(in.peek())) {
          int type = Integer.parseInt(in.nextString());
          CustomValueTypeAdapter<? extends Value> adapter = getCustomValueTypeAdapter(type);
          in.nextName();
          Value value = adapter.read(in);
          in.endObject();
          return value;
        } else {
          object.add(name, read(in));
        }
      }
      while (in.hasNext()) {
        object.add(in.nextName(), read(in));
      }
      in.endObject();
      return object;
    case END_DOCUMENT:
    case NAME:
    case END_OBJECT:
    case END_ARRAY:
    default:
      throw new IllegalArgumentException();
  }
}
 
Example #23
Source Project: blueflood   Author: rackerlabs   File: HttpAggregatedIngestionHandlerTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test(expected = NumberFormatException.class)
public void testExpectedGsonConversionFailure() {
    new LazilyParsedNumber("2.321").longValue();
}
 
Example #24
Source Project: gson   Author: google   File: JsonPrimitive.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * convenience method to get this element as a Number.
 *
 * @return get this element as a Number.
 * @throws NumberFormatException if the value contained is not a valid Number.
 */
@Override
public Number getAsNumber() {
  return value instanceof String ? new LazilyParsedNumber((String) value) : (Number) value;
}
 
Example #25
Source Project: activitystreams   Author: OpenSocial   File: LazilyParsedNumberComparable.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Constructor for LazilyParsedNumberComparable.
 * @param inner LazilyParsedNumber
 */
public LazilyParsedNumberComparable(LazilyParsedNumber inner) {
  this.inner = inner;
}
 
Example #26
Source Project: framework   Author: Odoo-mobile   File: JsonPrimitive.java    License: GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * convenience method to get this element as a Number.
 *
 * @return get this element as a Number.
 * @throws NumberFormatException if the value contained is not a valid Number.
 */
@Override
public Number getAsNumber() {
  return value instanceof String ? new LazilyParsedNumber((String) value) : (Number) value;
}