Java Code Examples for com.google.gson.stream.JsonToken#BOOLEAN

The following examples show how to use com.google.gson.stream.JsonToken#BOOLEAN . 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: IpcdJsonReader.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the type of the next token without consuming it.
 */
public JsonToken peek() throws IOException {
   int p = peeked;
   if (p == PEEKED_NONE) {
      p = doPeek();
   }

   switch (p) {
      case PEEKED_BEGIN_OBJECT:
         return JsonToken.BEGIN_OBJECT;
      case PEEKED_END_OBJECT:
         return JsonToken.END_OBJECT;
      case PEEKED_BEGIN_ARRAY:
         return JsonToken.BEGIN_ARRAY;
      case PEEKED_END_ARRAY:
         return JsonToken.END_ARRAY;
      case PEEKED_SINGLE_QUOTED_NAME:
      case PEEKED_DOUBLE_QUOTED_NAME:
      case PEEKED_UNQUOTED_NAME:
         return JsonToken.NAME;
      case PEEKED_TRUE:
      case PEEKED_FALSE:
         return JsonToken.BOOLEAN;
      case PEEKED_NULL:
         return JsonToken.NULL;
      case PEEKED_SINGLE_QUOTED:
      case PEEKED_DOUBLE_QUOTED:
      case PEEKED_UNQUOTED:
      case PEEKED_BUFFERED:
         return JsonToken.STRING;
      case PEEKED_LONG:
      case PEEKED_NUMBER:
         return JsonToken.NUMBER;
      case PEEKED_EOF:
         return JsonToken.END_DOCUMENT;
      default:
         throw new AssertionError();
   }
}
 
Example 2
Source File: WrapTypeAdapters.java    From SimpleProject with MIT License 5 votes vote down vote up
@Override
public String read(JsonReader in) throws IOException {
	JsonToken peek = in.peek();
	if (peek == JsonToken.NULL) {
		in.nextNull();
		return "";
	}

	if (peek == JsonToken.BOOLEAN) {
		return Boolean.toString(in.nextBoolean());
	}
	return in.nextString();
}
 
Example 3
Source File: TypeAdapters.java    From letv with Apache License 2.0 5 votes vote down vote up
public String read(JsonReader in) throws IOException {
    JsonToken peek = in.peek();
    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else if (peek == JsonToken.BOOLEAN) {
        return Boolean.toString(in.nextBoolean());
    } else {
        return in.nextString();
    }
}
 
Example 4
Source File: HistoryFragmentVM.java    From chaoli-forum-for-android-2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public HistoryItem.Data read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.BOOLEAN) {
        in.skipValue();
        return null;
    }
    return defaultAdapter.read(in);
}
 
Example 5
Source File: EitherTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
protected JsonToken getExpectedToken(Class<?> rawType) {
	if (rawType.isArray() || Collection.class.isAssignableFrom(rawType) || Tuple.class.isAssignableFrom(rawType)) {
		return JsonToken.BEGIN_ARRAY;
	}
	if (Boolean.class.isAssignableFrom(rawType)) {
		return JsonToken.BOOLEAN;
	}
	if (Number.class.isAssignableFrom(rawType) || Enum.class.isAssignableFrom(rawType)) {
		return JsonToken.NUMBER;
	}
	if (Character.class.isAssignableFrom(rawType) || String.class.isAssignableFrom(rawType)) {
		return JsonToken.STRING;
	}
	return JsonToken.BEGIN_OBJECT;
}
 
Example 6
Source File: TypeAdapters.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override
public String read(JsonReader in) throws IOException {
  JsonToken peek = in.peek();
  if (peek == JsonToken.NULL) {
    in.nextNull();
    return null;
  }
  /* coerce booleans to strings for backwards compatibility */
  if (peek == JsonToken.BOOLEAN) {
    return Boolean.toString(in.nextBoolean());
  }
  return in.nextString();
}
 
Example 7
Source File: TypeAdapters.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public String read(JsonReader in) throws IOException {
  JsonToken peek = in.peek();
  if (peek == JsonToken.NULL) {
    in.nextNull();
    return null;
  }
  /* coerce booleans to strings for backwards compatibility */
  if (peek == JsonToken.BOOLEAN) {
    return Boolean.toString(in.nextBoolean());
  }
  return in.nextString();
}
 
Example 8
Source File: BsonReader.java    From immutables with Apache License 2.0 5 votes vote down vote up
private static JsonToken toGsonToken(BsonType type) {
  switch (type) {
  case END_OF_DOCUMENT:
    return JsonToken.END_DOCUMENT;
  case DOCUMENT:
    return JsonToken.BEGIN_OBJECT;
  case ARRAY:
    return JsonToken.BEGIN_ARRAY;
  case BOOLEAN:
    return JsonToken.BOOLEAN;
  case STRING:
  case SYMBOL:
  case OBJECT_ID:
  case BINARY:
  case REGULAR_EXPRESSION:
    return JsonToken.STRING;
  case DATE_TIME:
  case DOUBLE:
  case INT32:
  case INT64:
  case TIMESTAMP:
  case DECIMAL128:
    return JsonToken.NUMBER;
  case NULL:
  case UNDEFINED:
    return JsonToken.NULL;
  default:
    // not really sure what to do with this type
    return JsonToken.NULL;
  }
}
 
Example 9
Source File: JsonParserReader.java    From immutables with Apache License 2.0 5 votes vote down vote up
private static JsonToken toGsonToken(com.fasterxml.jackson.core.JsonToken token) {
  switch (token) {
  case START_ARRAY:
    return JsonToken.BEGIN_ARRAY;
  case END_ARRAY:
    return JsonToken.END_ARRAY;
  case START_OBJECT:
    return JsonToken.BEGIN_OBJECT;
  case END_OBJECT:
    return JsonToken.END_OBJECT;
  case FIELD_NAME:
    return JsonToken.NAME;
  case VALUE_FALSE:
    return JsonToken.BOOLEAN;
  case VALUE_TRUE:
    return JsonToken.BOOLEAN;
  case VALUE_NULL:
    return JsonToken.NULL;
  case VALUE_NUMBER_INT:
    return JsonToken.NUMBER;
  case VALUE_NUMBER_FLOAT:
    return JsonToken.NUMBER;
  case VALUE_STRING:
  case VALUE_EMBEDDED_OBJECT:
    return JsonToken.STRING;
  default: // Not semantically equivalent
    return JsonToken.NULL;
  }
}
 
Example 10
Source File: JsonPropertyConsumer.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo, final Object typeMapping) throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE.addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, entityPropertyInfo.getFacets(), typeMappingClass);
}
 
Example 11
Source File: JsonTreeReader.java    From letv with Apache License 2.0 4 votes vote down vote up
public JsonToken peek() throws IOException {
    if (this.stack.isEmpty()) {
        return JsonToken.END_DOCUMENT;
    }
    Iterator<?> o = peekStack();
    if (o instanceof Iterator) {
        boolean isObject = this.stack.get(this.stack.size() - 2) instanceof JsonObject;
        Iterator<?> iterator = o;
        if (!iterator.hasNext()) {
            return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
        } else {
            if (isObject) {
                return JsonToken.NAME;
            }
            this.stack.add(iterator.next());
            return peek();
        }
    } else if (o instanceof JsonObject) {
        return JsonToken.BEGIN_OBJECT;
    } else {
        if (o instanceof JsonArray) {
            return JsonToken.BEGIN_ARRAY;
        }
        if (o instanceof JsonPrimitive) {
            JsonPrimitive primitive = (JsonPrimitive) o;
            if (primitive.isString()) {
                return JsonToken.STRING;
            }
            if (primitive.isBoolean()) {
                return JsonToken.BOOLEAN;
            }
            if (primitive.isNumber()) {
                return JsonToken.NUMBER;
            }
            throw new AssertionError();
        } else if (o instanceof JsonNull) {
            return JsonToken.NULL;
        } else {
            if (o == SENTINEL_CLOSED) {
                throw new IllegalStateException("JsonReader is closed");
            }
            throw new AssertionError();
        }
    }
}
 
Example 12
Source File: JsonTreeReader.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public JsonToken peek()
{
    if (c.isEmpty())
    {
        return JsonToken.END_DOCUMENT;
    }
    Object obj = a();
    if (obj instanceof Iterator)
    {
        boolean flag = c.get(-2 + c.size()) instanceof JsonObject;
        Iterator iterator = (Iterator)obj;
        if (iterator.hasNext())
        {
            if (flag)
            {
                return JsonToken.NAME;
            } else
            {
                c.add(iterator.next());
                return peek();
            }
        }
        if (flag)
        {
            return JsonToken.END_OBJECT;
        } else
        {
            return JsonToken.END_ARRAY;
        }
    }
    if (obj instanceof JsonObject)
    {
        return JsonToken.BEGIN_OBJECT;
    }
    if (obj instanceof JsonArray)
    {
        return JsonToken.BEGIN_ARRAY;
    }
    if (obj instanceof JsonPrimitive)
    {
        JsonPrimitive jsonprimitive = (JsonPrimitive)obj;
        if (jsonprimitive.isString())
        {
            return JsonToken.STRING;
        }
        if (jsonprimitive.isBoolean())
        {
            return JsonToken.BOOLEAN;
        }
        if (jsonprimitive.isNumber())
        {
            return JsonToken.NUMBER;
        } else
        {
            throw new AssertionError();
        }
    }
    if (obj instanceof JsonNull)
    {
        return JsonToken.NULL;
    }
    if (obj == b)
    {
        throw new IllegalStateException("JsonReader is closed");
    } else
    {
        throw new AssertionError();
    }
}
 
Example 13
Source File: JsonTreeReader.java    From gson with Apache License 2.0 4 votes vote down vote up
@Override public JsonToken peek() throws IOException {
  if (stackSize == 0) {
    return JsonToken.END_DOCUMENT;
  }

  Object o = peekStack();
  if (o instanceof Iterator) {
    boolean isObject = stack[stackSize - 2] instanceof JsonObject;
    Iterator<?> iterator = (Iterator<?>) o;
    if (iterator.hasNext()) {
      if (isObject) {
        return JsonToken.NAME;
      } else {
        push(iterator.next());
        return peek();
      }
    } else {
      return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
    }
  } else if (o instanceof JsonObject) {
    return JsonToken.BEGIN_OBJECT;
  } else if (o instanceof JsonArray) {
    return JsonToken.BEGIN_ARRAY;
  } else if (o instanceof JsonPrimitive) {
    JsonPrimitive primitive = (JsonPrimitive) o;
    if (primitive.isString()) {
      return JsonToken.STRING;
    } else if (primitive.isBoolean()) {
      return JsonToken.BOOLEAN;
    } else if (primitive.isNumber()) {
      return JsonToken.NUMBER;
    } else {
      throw new AssertionError();
    }
  } else if (o instanceof JsonNull) {
    return JsonToken.NULL;
  } else if (o == SENTINEL_CLOSED) {
    throw new IllegalStateException("JsonReader is closed");
  } else {
    throw new AssertionError();
  }
}
 
Example 14
Source File: JsonPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final EntityProviderReadProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}
 
Example 15
Source File: JsonPropertyDeserializer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
    final Object typeMapping, final DeserializerProperties readProperties)
    throws EdmException, EntityProviderException, IOException {
  final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
  Object value = null;
  final JsonToken tokenType = reader.peek();
  if (tokenType == JsonToken.NULL) {
    reader.nextNull();
  } else {
    switch (EdmSimpleTypeKind.valueOf(type.getName())) {
    case Boolean:
      if (tokenType == JsonToken.BOOLEAN) {
        value = reader.nextBoolean();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Byte:
    case SByte:
    case Int16:
    case Int32:
      if (tokenType == JsonToken.NUMBER) {
        value = reader.nextInt();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    case Single:
    case Double:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else if (tokenType == JsonToken.NUMBER) {
        value = reader.nextDouble();
        value = value.toString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    default:
      if (tokenType == JsonToken.STRING) {
        value = reader.nextString();
      } else {
        throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
            .addContent(entityPropertyInfo.getName()));
      }
      break;
    }
  }

  final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
  final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ?
      entityPropertyInfo.getFacets() : null;
  return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass);
}
 
Example 16
Source File: JsonTreeReader.java    From framework with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override public JsonToken peek() throws IOException {
  if (stack.isEmpty()) {
    return JsonToken.END_DOCUMENT;
  }

  Object o = peekStack();
  if (o instanceof Iterator) {
    boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject;
    Iterator<?> iterator = (Iterator<?>) o;
    if (iterator.hasNext()) {
      if (isObject) {
        return JsonToken.NAME;
      } else {
        stack.add(iterator.next());
        return peek();
      }
    } else {
      return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
    }
  } else if (o instanceof JsonObject) {
    return JsonToken.BEGIN_OBJECT;
  } else if (o instanceof JsonArray) {
    return JsonToken.BEGIN_ARRAY;
  } else if (o instanceof JsonPrimitive) {
    JsonPrimitive primitive = (JsonPrimitive) o;
    if (primitive.isString()) {
      return JsonToken.STRING;
    } else if (primitive.isBoolean()) {
      return JsonToken.BOOLEAN;
    } else if (primitive.isNumber()) {
      return JsonToken.NUMBER;
    } else {
      throw new AssertionError();
    }
  } else if (o instanceof JsonNull) {
    return JsonToken.NULL;
  } else if (o == SENTINEL_CLOSED) {
    throw new IllegalStateException("JsonReader is closed");
  } else {
    throw new AssertionError();
  }
}