下面列出了com.google.gson.stream.JsonToken#BOOLEAN 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
@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();
}
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();
}
}
@Override
public HistoryItem.Data read(JsonReader in) throws IOException {
if (in.peek() == JsonToken.BOOLEAN) {
in.skipValue();
return null;
}
return defaultAdapter.read(in);
}
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;
}
@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();
}
@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();
}
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;
}
}
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;
}
}
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);
}
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();
}
}
}
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();
}
}
@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();
}
}
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);
}
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);
}
@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();
}
}