jodd.json.JsonParser.parse()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(146)

本文整理了Java中jodd.json.JsonParser.parse()方法的一些代码示例,展示了JsonParser.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonParser.parse()方法的具体详情如下:
包路径:jodd.json.JsonParser
类名称:JsonParser
方法名:parse

JsonParser.parse介绍

[英]Parses input JSON string.
[中]解析输入JSON字符串。

代码示例

代码示例来源:origin: oblac/jodd

/**
 * Parses input JSON to {@link JsonArray}, special case of parsing.
 */
public JsonArray parseAsJsonArray(final String input) {
  return new JsonArray(parse(input));
}

代码示例来源:origin: oblac/jodd

/**
 * Parses request body into the target type.
 */
protected Object parseRequestBody(final String body, final Class targetType) {
  return JsonParser.create().parse(body, targetType);
}

代码示例来源:origin: oblac/jodd

/**
 * Parses input JSON to {@link JsonObject}, special case of {@link #parse(String, Class)}.
 */
public JsonObject parseAsJsonObject(final String input) {
  return new JsonObject(parse(input));
}

代码示例来源:origin: oblac/jodd

/**
 * Parses input JSON to a list with specified component type.
 */
public <T> List<T> parseAsList(final String string, final Class<T> componentType) {
  return new JsonParser()
    .map(JsonParser.VALUES, componentType)
    .parse(string);
}

代码示例来源:origin: oblac/jodd

/**
 * Parses input JSON to a list with specified key and value types.
 */
public <K, V> Map<K, V> parseAsMap(
  final String string, final Class<K> keyType, final Class<V> valueType) {
  return new JsonParser()
    .map(JsonParser.KEYS, keyType)
    .map(JsonParser.VALUES, valueType)
    .parse(string);
}

代码示例来源:origin: oblac/jodd

@Test
void testSimpleConversions() {
  JsonParsers.forEachParser(jsonParser -> {
    assertEquals(173, jsonParser.parse("\"173\"", Integer.class).intValue());
    assertEquals("123", jsonParser.parse("123", String.class));
    assertEquals(1, jsonParser.parse("true", Integer.class).intValue());
    assertEquals(0, jsonParser.parse("false", Integer.class).intValue());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testConversionsToObject() {
  JsonParsers.forEachParser(jsonParser -> {
    assertEquals("173", jsonParser.parse("\"173\"", Object.class));
    assertEquals(123, jsonParser.parse("123", Object.class));
    assertEquals(true, jsonParser.parse("true", Object.class));
    assertTrue(jsonParser.parse("[]", Object.class) instanceof List);
    assertTrue(jsonParser.parse("{}", Object.class) instanceof Map);
  });
}

代码示例来源:origin: oblac/jodd

@Test
  void testLazyParserSupportEscapedDoubleQuotes() {
    String json = "{ \"values\": [{ \"value\": \"foo\\\"bar\" }]}";

    JsonParsers.forEachParser(jsonParser -> {
      Map<String, Object> object = jsonParser.parse(json);

      List<Map.Entry<String, Object>> entries = object.entrySet().stream().collect(Collectors.toList());

      assertEquals(1, entries.size());
      assertEquals("values", entries.get(0).getKey());
      assertEquals("foo\"bar", ((List<Map<String, String>>) entries.get(0).getValue()).get(0).get("value"));
    });
  }
}

代码示例来源:origin: oblac/jodd

@Test
void testStringEscapes() {
  JsonParsers.forEachParser(jsonParser -> {
    assertEquals("\n4", jsonParser.parse("\"\\n\\u0034\""));
    try {
      jsonParser.parse("\"\\u034\"");
      fail("error");
    } catch (Exception ignore) {
    }
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testGeneralMapDeserialization2() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = new JsonSerializer().serialize(creator.createJodder());
    Map<String, Object> deserialized = jsonParser.parse(json);
    assertEquals("Igor", deserialized.get("firstname"));
    assertEquals("Spasic", deserialized.get("lastname"));
    assertTrue(Map.class.isAssignableFrom(deserialized.get("work").getClass()));
    assertTrue(Map.class.isAssignableFrom(deserialized.get("home").getClass()));
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testEscapeAtTheEndOfLongString() {
  JsonParsers.forEachParser(jsonParser -> {
    String s = StringUtil.repeat('A', 800);
    String json = "\"" + s + "\\n\"";
    try {
      jsonParser.parse(json);
    } catch (Exception ex) {
      fail(ex.toString());
    }
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testParseCatalogAsObject() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = loadJSON("citm_catalog");
    Catalog catalog = jsonParser.parse(json, Catalog.class);
    assertCatalog(catalog);
  });
}

代码示例来源:origin: oblac/jodd

@Test
public void testDouble_NaN() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = JsonSerializer.create().serialize(Double.NaN);
    assertEquals("\"NaN\"", json);
    Double d = jsonParser.parse(json, Double.class);
    assertTrue(d.isNaN());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testJsonValueContextArray2() {
  JsonParsers.forEachParser(jsonParser -> {
    Object[] array = new Object[]{new Surfer(), "two", "three"};
    JsonSerializer jsonSerializer = new JsonSerializer()
      .withSerializer(String.class, new MyTypeJsonSerializer2());
    String json = jsonSerializer.serialize(array);
    List<String> list = jsonParser.parse(json);
    assertEquals(3, list.size());
    assertEquals("TWO", list.get(1));
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testDeserializeWithIncludes() {
  JsonParsers.forEachParser(jsonParser -> {
    Person igor = creator.createJodder();
    String json = new JsonSerializer().include("phones", "hobbies").serialize(igor);
    Person jsonIgor = jsonParser.parse(json, Person.class);
    assertEquals(2, jsonIgor.getPhones().size());
    assertEquals(0, jsonIgor.getHobbies().size());
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testEscapeChars() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = "\"1\\\" 2\\\\ 3\\/ 4\\b 5\\f 6\\n 7\\r 8\\t\"";
    String str = jsonParser.parse(json);
    assertEquals("1\" 2\\ 3/ 4\b 5\f 6\n 7\r 8\t", str);
    String jsonStr = new JsonSerializer().strictStringEncoding(true).serialize(str);
    assertEquals(json, jsonStr);
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testPoint() {
  JsonParser.Defaults.classMetadataName = "__class";
  JsonSerializer.Defaults.classMetadataName = "__class";
  JsonParsers.forEachParser(jsonParser -> {
    String json = new JsonSerializer().serialize(new Point2D.Float(1.0f, 2.0f));
    Point2D.Float point = jsonParser.parse(json);
    assertEquals(1.0f, point.x, DELTA);
    assertEquals(2.0f, point.y, DELTA);
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testDeserializeInterfaces2() {
  JsonParsers.forEachParser(jsonParser -> {
    Hero superman = creator.createSuperman();
    String json = new JsonSerializer().include("powers").withClassMetadata(true).serialize(superman);
    Hero jsonSuperMan = jsonParser.withClassMetadata(true).parse(json, Hero.class);
    assertNotNull(jsonSuperMan);
    assertEquals(4, jsonSuperMan.getPowers().size());
    assertHeroHasSuperPowers(jsonSuperMan);
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testDeserializeInterfaces() {
  JsonParsers.forEachParser(jsonParser -> {
    Hero superman = creator.createSuperman();
    String json = new JsonSerializer().include("powers").setClassMetadataName("class").serialize(superman);
    Hero jsonSuperMan = jsonParser.setClassMetadataName("class").parse(json, Hero.class);
    assertNotNull(jsonSuperMan);
    assertEquals(4, jsonSuperMan.getPowers().size());
    assertHeroHasSuperPowers(jsonSuperMan);
  });
}

代码示例来源:origin: oblac/jodd

@Test
void testSubClassDeserialize() {
  JsonParsers.forEachParser(jsonParser -> {
    Employee dilbert = creator.createDilbert();
    String json = new JsonSerializer().include("phones", "hobbies").serialize(dilbert);
    Person jsonDilbert = jsonParser.parse(json, Employee.class);
    assertNotNull(jsonDilbert);
    assertTrue(jsonDilbert instanceof Employee);
    assertEquals(dilbert.getCompany(), ((Employee) jsonDilbert).getCompany());
  });
}

相关文章