Java Code Examples for com.fasterxml.jackson.core.JsonToken#VALUE_STRING

The following examples show how to use com.fasterxml.jackson.core.JsonToken#VALUE_STRING . 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: JsonDatabaseReader.java    From dbsync with Apache License 2.0 6 votes vote down vote up
Map<String, Object> readRecord(JsonParser jp) throws IOException {

        Map<String, Object> record = new HashMap<>();
        String fielName =  "";
        Object value;
        JsonToken token;

        while((token = jp.nextToken()) != JsonToken.END_OBJECT) {
            if (token == JsonToken.FIELD_NAME) {
                fielName = jp.getCurrentName();
            } else if (token == JsonToken.VALUE_STRING) {
                value = jp.getValueAsString();
                record.put(fielName, value);
            } else if (token == JsonToken.VALUE_NUMBER_INT) {
                value = jp.getValueAsLong();
                record.put(fielName, value);
            }
        }

        //System.out.println(record.toString());
        return record;
    }
 
Example 2
Source File: JsonldBeanDeserializerModifier.java    From jackson-jsonld with MIT License 6 votes vote down vote up
private Object parseJsonldObject(JsonParser jp) throws IOException {
    Object rval = null;
    final JsonToken initialToken = jp.getCurrentToken();

    if (initialToken == JsonToken.START_ARRAY) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(List.class);
    } else if (initialToken == JsonToken.START_OBJECT) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(Map.class);
    } else if (initialToken == JsonToken.VALUE_STRING) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(String.class);
    } else if (initialToken == JsonToken.VALUE_FALSE || initialToken == JsonToken.VALUE_TRUE) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(Boolean.class);
    } else if (initialToken == JsonToken.VALUE_NUMBER_FLOAT
            || initialToken == JsonToken.VALUE_NUMBER_INT) {
        jp.setCodec(mapper);
        rval = jp.readValueAs(Number.class);
    } else if (initialToken == JsonToken.VALUE_NULL) {
        rval = null;
    }
    return rval;
}
 
Example 3
Source File: YearDeserializer.java    From jackson-modules-java8 with Apache License 2.0 6 votes vote down vote up
@Override
public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException
{
    JsonToken t = parser.currentToken();
    if (t == JsonToken.VALUE_STRING) {
        String string = parser.getValueAsString().trim();
        try {
            if (_formatter == null) {
                return Year.parse(string);
            }
            return Year.parse(string, _formatter);
        } catch (DateTimeException e) {
            return _handleDateTimeFormatException(context, e, _formatter, string);
        }
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return Year.of(parser.getIntValue());
    }
    if (t == JsonToken.VALUE_EMBEDDED_OBJECT) {
        return (Year) parser.getEmbeddedObject();
    }
    if (parser.hasToken(JsonToken.START_ARRAY)){
        return _deserializeFromArray(parser, context);
    }
    return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT);
}
 
Example 4
Source File: DirectionDeserializer.java    From swagger-inflector with Apache License 2.0 6 votes vote down vote up
@Override
public Set<Configuration.Direction> deserialize(JsonParser jp,
        DeserializationContext ctxt) throws IOException {
    final JsonToken token = jp.getCurrentToken();
    if (token == JsonToken.VALUE_FALSE) {
        return EnumSet.noneOf(Configuration.Direction.class);
    } else if (token == JsonToken.VALUE_TRUE) {
        return EnumSet.allOf(Configuration.Direction.class);
    } else if (token == JsonToken.START_ARRAY) {
        final Set<Configuration.Direction> items = EnumSet.noneOf(Configuration.Direction.class);
        while (true) {
            final JsonToken next = jp.nextToken();
            if (next == JsonToken.VALUE_STRING) {
                final String name = jp.getText();
                items.add(Configuration.Direction.valueOf(name));
            } else if (next == JsonToken.END_ARRAY) {
                return items;
            } else {
                break;
            }
        }
    }
    throw ctxt.mappingException(Configuration.Direction.class);
}
 
Example 5
Source File: CustomLocalDateTimeDeserializer.java    From mall with Apache License 2.0 6 votes vote down vote up
@Override
public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        if (DATE_TIME_FORMATTER_PATTERN.length() == str.length()) {
            return LocalDateTime.parse(str, DATE_TIME_FORMATTER);
        } else if (DATE_TIME_FORMATTER_MILLI_PATTERN.length() == str.length()) {
            return LocalDateTime.parse(str, DATE_TIME_FORMATTER_MILLI);
        }
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new Timestamp(jp.getLongValue()).toLocalDateTime();
    }
    throw ctxt.mappingException(handledType());
}
 
Example 6
Source File: DynamicDispatchAdapter.java    From ig-json-parser with MIT License 6 votes vote down vote up
public T parseFromJson(JsonParser parser) throws IOException {
  if (parser.getCurrentToken() != JsonToken.START_ARRAY) {
    parser.skipChildren();
    return null;
  }

  parser.nextToken();
  if (parser.getCurrentToken() != JsonToken.VALUE_STRING) {
    parser.skipChildren();
    return null;
  }

  String typeName = parser.getText();
  parser.nextToken();
  final T instance = getAdapter(typeName).parseFromJson(parser);
  parser.nextToken();
  return instance;
}
 
Example 7
Source File: FooJsonComponent.java    From spring-test-examples with Apache License 2.0 6 votes vote down vote up
@Override
public Foo deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  JsonToken t = p.getCurrentToken();

  if (t == JsonToken.VALUE_STRING) {
    String trim = p.getText().trim();

    String[] split = trim.split(",");
    Foo foo = new Foo();
    foo.setName(split[0].split("=")[1]);
    foo.setAge(Integer.parseInt(split[1].split("=")[1]));
    return foo;
  }

  return (Foo) ctxt.handleUnexpectedToken(handledType(), p);

}
 
Example 8
Source File: EthLog.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<LogResult> deserialize(
        JsonParser jsonParser,
        DeserializationContext deserializationContext) throws IOException {

    List<LogResult> logResults = new ArrayList<>();
    JsonToken nextToken = jsonParser.nextToken();

    if (nextToken == JsonToken.START_OBJECT) {
        Iterator<LogObject> logObjectIterator =
                objectReader.readValues(jsonParser, LogObject.class);
        while (logObjectIterator.hasNext()) {
            logResults.add(logObjectIterator.next());
        }
    } else if (nextToken == JsonToken.VALUE_STRING) {
        jsonParser.getValueAsString();

        Iterator<Hash> transactionHashIterator =
                objectReader.readValues(jsonParser, Hash.class);
        while (transactionHashIterator.hasNext()) {
            logResults.add(transactionHashIterator.next());
        }
    }
    return logResults;
}
 
Example 9
Source File: ISO8601LocalDateDeserializer.java    From angularjs-springboot-bookstore with MIT License 5 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        return ISODateTimeFormat.dateTimeParser().parseLocalDate(str);
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new LocalDate(jp.getLongValue());
    }
    throw ctxt.mappingException(handledType());
}
 
Example 10
Source File: Int128Module.java    From light-eventuate-4j with Apache License 2.0 5 votes vote down vote up
public Int128 deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
  JsonToken token = jp.getCurrentToken();
  if (token == JsonToken.VALUE_STRING) {
    String str = jp.getText().trim();
    if (str.isEmpty())
      return null;
    else
      return Int128.fromString(str);
  } else
    throw ctxt.mappingException(getValueClass());
}
 
Example 11
Source File: GroupsSerialization.java    From heroic with Apache License 2.0 5 votes vote down vote up
@Override
public Groups deserialize(JsonParser p, DeserializationContext c) throws IOException {
    /* fallback to default parser if object */
    if (p.getCurrentToken() == JsonToken.START_ARRAY) {
        final List<String> groups = p.readValueAs(LIST_OF_STRINGS);
        return new Groups(ImmutableSet.copyOf(groups));
    }

    if (p.getCurrentToken() == JsonToken.VALUE_STRING) {
        return new Groups(ImmutableSet.of(p.getText()));
    }

    throw c.wrongTokenException(p, JsonToken.START_ARRAY, null);
}
 
Example 12
Source File: FilterRegistry.java    From heroic with Apache License 2.0 5 votes vote down vote up
private Filter deserializeArray(final JsonParser p, final DeserializationContext c)
    throws IOException {
    if (p.nextToken() != JsonToken.VALUE_STRING) {
        throw c.mappingException("Expected operator (string)");
    }

    final String operator = p.readValueAs(String.class);

    final FilterEncoding<? extends Filter> deserializer = deserializers.get(operator);

    if (deserializer == null) {
        throw c.mappingException("No such operator: " + operator);
    }

    p.nextToken();

    final FilterEncoding.Decoder d = new Decoder(p, c);

    final Filter filter;

    try {
        filter = deserializer.deserialize(d);

        if (p.getCurrentToken() != JsonToken.END_ARRAY) {
            throw c.mappingException("Expected end of array from '" + deserializer + "'");
        }

        if (filter instanceof RawFilter) {
            return parseRawFilter((RawFilter) filter);
        }

        return filter.optimize();
    } catch (final Exception e) {
        // use special {operator} syntax to indicate filter.
        throw JsonMappingException.wrapWithPath(e, this, "{" + operator + "}");
    }
}
 
Example 13
Source File: TimeSeriesNameNodeCalc.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
static NodeCalc parseJson(JsonParser parser) throws IOException {
    JsonToken token;
    while ((token = parser.nextToken()) != null) {
        if (token == JsonToken.VALUE_STRING) {
            return new TimeSeriesNameNodeCalc(parser.getValueAsString());
        } else {
            throw NodeCalc.createUnexpectedToken(token);
        }
    }
    throw new TimeSeriesException("Invalid time series name node calc JSON");
}
 
Example 14
Source File: CustomProtobufParser.java    From caravan with Apache License 2.0 5 votes vote down vote up
/**
   * Method called to finish parsing of a token so that token contents
   * are retriable
   */
  @Override
protected void _finishToken() throws IOException
  {
    _tokenIncomplete = false;

    if (_currToken == JsonToken.VALUE_STRING) {
      final int len = _decodedLength;
      if (len > _inputEnd - _inputPtr) {
        // or if not, could we read?
        if (len >= _inputBuffer.length) {
          // If not enough space, need different handling
          _finishLongText(len);
          return;
        }
        _loadToHaveAtLeastCtrip(len);
      }
      // offline for better optimization
      _finishShortText(len);
      return;
    }
    if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
      _binaryValue = _finishBytes(_decodedLength);
      return;
    }
    // should never happen but:
    _throwInternal();
  }
 
Example 15
Source File: StdCallbackContext.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
private Object readEncoded(JsonParser p, DeserializationContext ctxt) throws IOException {
    if (!p.isExpectedStartArrayToken()) {
        throw new JsonParseException(p, "Expected array for encoded object got " + p.currentToken());
    }

    Object value = null;
    JsonToken next = p.nextToken();
    if (next != JsonToken.VALUE_STRING) {
        throw new JsonParseException(p, "Encoded Class value not present " + next);
    }
    String typeName = p.getText();
    p.nextToken(); // fwd to next
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    if (loader == null) {
        loader = getClass().getClassLoader();
    }
    try {
        Class<?> type = loader.loadClass(typeName);
        if (Collection.class.isAssignableFrom(type)) {
            value = readCollection(type, p, ctxt);
        } else if (Map.class.isAssignableFrom(type)) {
            value = readMap(type, p, ctxt);
        } else {
            JsonDeserializer<Object> deser = ctxt.findRootValueDeserializer(ctxt.constructType(type));
            value = deser.deserialize(p, ctxt);
        }
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(p, "Type name encoded " + typeName + " could not be loaded", e);
    }
    if (p.nextToken() != JsonToken.END_ARRAY) {
        throw new JsonParseException(p, "Encoded expected end of ARRAY marker " + p.currentToken());
    }
    return value;
}
 
Example 16
Source File: ButtonCollectionDeserializer.java    From OneSignal-Java-SDK with Apache License 2.0 5 votes vote down vote up
@Override
public List<Button> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
    ObjectMapper mapper = (ObjectMapper) p.getCodec();
    TreeNode treeNode = mapper.readTree(p);
    Button[] buttons;
    if (treeNode.asToken() == JsonToken.VALUE_STRING) {
        String rawJson = ((JsonNode) treeNode).asText();
        buttons = mapper.readValue(rawJson, Button[].class);
    } else {
        buttons = mapper.readValue(p, Button[].class);
    }
    return buttons == null ? Collections.<Button>emptyList() : Arrays.asList(buttons);
}
 
Example 17
Source File: ISO8601LocalDateDeserializer.java    From ServiceCutter with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDate deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException {
    JsonToken t = jp.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String str = jp.getText().trim();
        return ISODateTimeFormat.dateTimeParser().parseDateTime(str).toLocalDate();
    }
    if (t == JsonToken.VALUE_NUMBER_INT) {
        return new LocalDate(jp.getLongValue());
    }
    throw ctxt.mappingException(handledType());
}
 
Example 18
Source File: DbxHost.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public DbxHost read(JsonParser parser) throws IOException, JsonReadException {
    JsonToken t = parser.getCurrentToken();
    if (t == JsonToken.VALUE_STRING) {
        String s = parser.getText();
        JsonReader.nextToken(parser);
        return DbxHost.fromBaseHost(s);
    } else if (t == JsonToken.START_OBJECT) {
        JsonLocation top = parser.getTokenLocation();
        nextToken(parser);

        String api = null;
        String content = null;
        String web = null;
        String notify = null;

        while (parser.getCurrentToken() == JsonToken.FIELD_NAME) {
            String fieldName = parser.getCurrentName();
            parser.nextToken();

            try {
                if (fieldName.equals("api")) {
                    api = JsonReader.StringReader.readField(parser, fieldName, api);
                }
                else if (fieldName.equals("content")) {
                    content = JsonReader.StringReader.readField(parser, fieldName, content);
                }
                else if (fieldName.equals("web")) {
                    web = JsonReader.StringReader.readField(parser, fieldName, web);
                }
                else if (fieldName.equals("notify")) {
                    notify = JsonReader.StringReader.readField(parser, fieldName, notify);
                }
                else {
                    throw new JsonReadException("unknown field", parser.getCurrentLocation());
                }
            }
            catch (JsonReadException ex) {
                throw ex.addFieldContext(fieldName);
            }
        }

        JsonReader.expectObjectEnd(parser);

        if (api == null) throw new JsonReadException("missing field \"api\"", top);
        if (content == null) throw new JsonReadException("missing field \"content\"", top);
        if (web == null) throw new JsonReadException("missing field \"web\"", top);
        if (notify == null) throw new JsonReadException("missing field \"notify\"", top);

        return new DbxHost(api, content, web, notify);
    } else {
        throw new JsonReadException("expecting a string or an object", parser.getTokenLocation());
    }
}
 
Example 19
Source File: YearSerializer.java    From jackson-modules-java8 with Apache License 2.0 4 votes vote down vote up
@Override // since 2.9
protected JsonToken serializationShape(SerializerProvider provider) {
    return useTimestamp(provider) ? JsonToken.VALUE_NUMBER_INT : JsonToken.VALUE_STRING;
}
 
Example 20
Source File: EmoCoordinate.java    From emodb with Apache License 2.0 4 votes vote down vote up
@Override
protected Text narrow(JsonParser parser)
        throws IOException {
    if (parser.getCurrentToken() != JsonToken.START_OBJECT) {
        return null;
    }

    String id = null;
    String table = null;
    JsonToken currentToken = parser.nextToken();

    while ((id == null || table == null) && currentToken != JsonToken.END_OBJECT) {
        if (currentToken != JsonToken.FIELD_NAME) {
            // This should always be a field.  Something is amiss.
            throw new IOException("Field not found at expected location");
        }
        String fieldName = parser.getText();
        currentToken = parser.nextToken();

        if (id == null && fieldName.equals(Intrinsic.ID)) {
            if (currentToken != JsonToken.VALUE_STRING) {
                return null;
            }
            id = parser.getValueAsString();
            currentToken = parser.nextToken();
        } else if (table == null && fieldName.equals(Intrinsic.TABLE)) {
            if (currentToken != JsonToken.VALUE_STRING) {
                return null;
            }
            table = parser.getValueAsString();
            currentToken = parser.nextToken();
        } else {
            currentToken = skipValue(parser);
        }
    }

    if (id == null || table == null) {
        return null;
    }

    return new Text(Coordinate.of(table, id).toString());
}