jodd.json.JsonSerializer.create()方法的使用及代码示例

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

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

JsonSerializer.create介绍

[英]Static ctor.
[中]静态选择器。

代码示例

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

/**
 * Returns JSON serialized by default {@link JsonSerializer}.
 */
@Override
public String toString() {
  return JsonSerializer.create().deep(true).serialize(map);
}

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

@Override
public String toString() {
  return JsonSerializer.create().deep(true).serialize(this);
}

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

/**
 * Creates a JSON response from an exception. Response body will have information about the
 * exception and response status will be set to 500.
 */
public static JsonResult of(final Exception exception) {
  final HashMap<String, Object> errorMap = new HashMap<>();
  errorMap.put("message", ExceptionUtil.message(exception));
  errorMap.put("error", exception.getClass().getName());
  errorMap.put("cause", exception.getCause() != null ? exception.getCause().getClass().getName() : null);
  final ArrayList<String> details = new ArrayList<>();
  final StackTraceElement[] ste = ExceptionUtil.getStackTrace(exception, null, null);
  for (StackTraceElement stackTraceElement : ste) {
    details.add(stackTraceElement.toString());
  }
  errorMap.put("details", details);
  final String json = JsonSerializer.create().deep(true).serialize(errorMap);
  return new JsonResult(json).status(HttpStatus.error500().internalError());
}

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

/**
 * Creates JSON result from given object. The object will be serialized to JSON.
 */
public static JsonResult of(final Object object) {
  final String json = JsonSerializer.create().deep(true).serialize(object);
  return new JsonResult(json);
}

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

/**
 * Encodes the {@link SimTok} to JSON string.
 */
public String encode(final SimTok simTok) {
  final String json = JsonSerializer.create().deep(true).serialize(simTok);
  final String p1 = Base64.encodeToString("JoddSimTok" + SALT_ROUNDS);
  final String p2 = Base64.encodeToString(json);
  final String salt = BCrypt.gensalt(SALT_ROUNDS);
  final String p3 = BCrypt.hashpw(p1 + "." + p2 + "." + SECRET, salt);
  return p1 + "." + p2 + "." + p3;
}

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

@Test
  void testBooleanField() {
    Active active = new Active();

    String json = JsonSerializer.create().serialize(active);

    System.out.println(json);

    assertTrue(json.contains("\"active\":true"));
    assertTrue(json.contains("\"inactive\":3"));

  }
}

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

@Test
void testClassMetaData() {
  String json = JsonSerializer
    .create()
    .withClassMetadata(true)
    .serialize(new Foo());
  assertTrue(json.contains("\"__class\":\"" + Foo.class.getName() + "\""));
  json = JsonSerializer.create().withClassMetadata(false).serialize(123);
  assertEquals("123", json);
  json = JsonSerializer.create().withClassMetadata(true).serialize(123);
  assertEquals("123", json);
}

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

@Test
public void testDouble_Infinity() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = JsonSerializer.create().serialize(Double.POSITIVE_INFINITY);
    assertEquals("\"+Infinity\"", json);
    Double d = jsonParser.parse(json, Double.class);
    assertEquals(Double.POSITIVE_INFINITY, d.doubleValue());
    json = JsonSerializer.create().serialize(Double.NEGATIVE_INFINITY);
    assertEquals("\"-Infinity\"", json);
    d = jsonParser.parse(json, Double.class);
    assertEquals(Double.NEGATIVE_INFINITY, d.doubleValue());
  });
}

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

@Test
  void testUuidSerialization() {
    UUID uuid = UUID.randomUUID();

    String json = JsonSerializer
      .create()
      .serialize(uuid);

    UUID uuid2 = JsonParser
      .create()
      .parse(json, UUID.class);

    assertEquals(uuid, uuid2);
  }
}

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

@Test
public void testFloat_Infinity() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = JsonSerializer.create().serialize(Float.POSITIVE_INFINITY);
    assertEquals("\"+Infinity\"", json);
    Float d = jsonParser.parse(json, Float.class);
    assertEquals(Float.POSITIVE_INFINITY, d.floatValue());
    json = JsonSerializer.create().serialize(Float.NEGATIVE_INFINITY);
    assertEquals("\"-Infinity\"", json);
    d = jsonParser.parse(json, Float.class);
    assertEquals(Float.NEGATIVE_INFINITY, d.floatValue());
  });
}

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

@Test
void testSerializeSets() {
  HitList hitList = new HitList();
  hitList.setNames(new HashSet<>());
  hitList.getNames().add("Joe");
  hitList.getNames().add("Pig");
  hitList.setNumbers(new HashSet<>());
  hitList.getNumbers().add(173);
  hitList.getNumbers().add(22);
  String json = JsonSerializer
    .create()
    .deep(true)
    .serialize(hitList);
  assertTrue(json.contains("\"names\""));
  assertTrue(json.contains("\"numbers\""));
  assertTrue(json.contains("\"Pig\""));
  assertTrue(json.contains("\"Joe\""));
  assertTrue(json.contains("173"));
  assertTrue(json.contains("22"));
}

代码示例来源: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
public void testFloat_NaN() {
  JsonParsers.forEachParser(jsonParser -> {
    String json = JsonSerializer.create().serialize(Float.NaN);
    assertEquals("\"NaN\"", json);
    Float d = jsonParser.parse(json, Float.class);
    assertTrue(d.isNaN());
  });
}

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

@Test
@EnabledOnOs(value = {OS.WINDOWS})
void testFiles_on_windows() {
  FileMan fileMan = new FileMan();
  File userHome = new File(SystemUtil.info().getHomeDir());
  fileMan.setFile(userHome);
  final String json = JsonSerializer.create().serialize(fileMan);
  // C:\Users\xxxx will be user home on windows hsost;  char '\' is escpaed in json therefore the execution of "String#replace"
  final String userhome_escpaed = SystemUtil.info().getHomeDir().replace("\\", "\\\\");
  assertTrue(json.contains(userhome_escpaed));
}

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

@Test
@EnabledOnOs(value = {OS.AIX, OS.LINUX, OS.MAC, OS.SOLARIS})
void testFiles_on_linux() {
  FileMan fileMan = new FileMan();
  File userHome = new File(SystemUtil.info().getHomeDir());
  fileMan.setFile(userHome);
  String json = JsonSerializer.create().serialize(fileMan);
  assertTrue(json.contains(SystemUtil.info().getHomeDir()));
}

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

@Test
void testToString() {
  jsonArray.add("foo").add(123);
  assertEquals(JsonSerializer.create().serialize(jsonArray), jsonArray.toString());
}

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

@Test
void testToString() {
  JsonObject jsonObject = new JsonObject();
  jsonObject.put("foo", "bar");
  assertEquals(JsonSerializer.create().serialize(jsonObject), jsonObject.toString());
}

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

@Test
void testSerializeStringEscapes() {
  JsonParsers.forEachParser(jsonParser -> {
    String path = "/foo/bar";
    String json = JsonSerializer
      .create()
      .strictStringEncoding(true)
      .serialize(path);
    assertEquals("\"\\/foo\\/bar\"", json);
    String path2 = jsonParser.parse(json);
    assertEquals(path, path2);
  });
}

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

@Test
void testEncode() throws Exception {
  jsonArray.add("foo");
  jsonArray.add(123);
  jsonArray.add(1234L);
  jsonArray.add(1.23f);
  jsonArray.add(2.34d);
  jsonArray.add(true);
  byte[] bytes = randomByteArray(10);
  jsonArray.add(bytes);
  jsonArray.addNull();
  jsonArray.add(new JsonObject().put("foo", "bar"));
  jsonArray.add(new JsonArray().add("foo").add(123));
  String strBytes = Base64.getEncoder().encodeToString(bytes);
  String expected = "[\"foo\",123,1234,1.23,2.34,true,\"" + strBytes + "\",null,{\"foo\":\"bar\"},[\"foo\",123]]";
  String json = JsonSerializer.create().serialize(jsonArray);
  assertEquals(expected, json);
}

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

@Test
void testEncode() {
  JsonParsers.forEachParser(jsonParser -> {
    JsonObject jsonObject = new JsonObject();
    jsonObject.put("mystr", "foo");
    jsonObject.put("mycharsequence", new StringBuilder("oob"));
    jsonObject.put("myint", 123);
    jsonObject.put("mylong", 1234L);
    jsonObject.put("myfloat", 1.23f);
    jsonObject.put("mydouble", 2.34d);
    jsonObject.put("myboolean", true);
    byte[] bytes = randomByteArray(10);
    jsonObject.put("mybinary", bytes);
    jsonObject.putNull("mynull");
    jsonObject.put("myobj", new JsonObject().put("foo", "bar"));
    jsonObject.put("myarr", new JsonArray().add("foo").add(123));
    String json = JsonSerializer.create().serialize(jsonObject);
    JsonObject expectedParsedJsonObject = jsonParser.parseAsJsonObject(json);
    // need to replace float with double, as decoding will do so
    jsonObject.put("myfloat", 1.23d);
    assertEquals(expectedParsedJsonObject, jsonObject);
  });
}

相关文章