JsonMapper { public String toJson(Object body) { for (int i = 1; ; i++) { try { return this.compact.toJson(body); } catch (IllegalStateException e) { handleIllegalStateException(e, i); } } } JsonMapper();  JsonMapper(Consumer<GsonBuilder> gsonConfigCallback);  JsonMapper(Gson compact, Gson compactSensitive); String toJson(Object body); void toJson(Object body, Appendable appendable); JsonElement toJsonElement(Object body); String toJsonHtml(Object body); void toJson(Set<JsonOptions> options, Object body, Appendable appendable); T fromJson(Object json, Class<T> clazz); T fromJson(Object json, Type type); void setJsonSuppressGsonSerializationErrors(boolean suppressErrors); static GsonBuilder createDefaultGsonBuilder(EnumSet<JsonOptions> options); void toJsonHtml(Object body, Appendable appendable); long hashJson(Object body, long seed);  }
@Test public void testConcurrency() throws InterruptedException { final ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); Query q = Builder.create() .addFieldClause( "kind", "foo") .addFieldClause( "name", "jane") .build(); for (int i = 0; i < 1000; i++) { final CountDownLatch start = new CountDownLatch(1); final TestContext finish = new TestContext(NUM_THREADS, Duration.ofSeconds(30)); final JsonMapper mapper = new JsonMapper(); for (int j = 0; j < NUM_THREADS; j++) { executor.execute(() -> { try { start.await(); mapper.toJson(q); finish.complete(); } catch (Throwable t) { finish.fail(t); } }); } start.countDown(); finish.await(); } }
@Test public void testJsonOptions() { TestGsonConfiguration.AnnotatedDoc testDoc = new TestGsonConfiguration.AnnotatedDoc(); testDoc.value = new TestGsonConfiguration.SomeComplexObject("complexA", "complexB"); testDoc.sensitivePropertyOptions = "sensitive data1"; testDoc.sensitiveUsageOption = "sensitive data2"; Pattern containsWhitespacePattern = Pattern.compile("\\s"); String jsonIncludeSensitiveAndBuiltInPrettyPrinted = Utils.toJson(EnumSet.noneOf(JsonMapper.JsonOptions.class), testDoc); assertThat(jsonIncludeSensitiveAndBuiltInPrettyPrinted, containsString("complexA")); assertThat(jsonIncludeSensitiveAndBuiltInPrettyPrinted, containsString("sensitive")); assertThat(jsonIncludeSensitiveAndBuiltInPrettyPrinted, containsString(ServiceDocument.FIELD_NAME_VERSION)); assertTrue("pretty-printed JSON should have whitespaces", containsWhitespacePattern.matcher(jsonIncludeSensitiveAndBuiltInPrettyPrinted).find()); String jsonExcludeSensitiveIncludesBuiltIn = Utils.toJson(EnumSet.of(JsonMapper.JsonOptions.EXCLUDE_SENSITIVE), testDoc); assertThat(jsonExcludeSensitiveIncludesBuiltIn, containsString("complexA")); assertThat(jsonExcludeSensitiveIncludesBuiltIn, not(containsString("sensitive"))); assertThat(jsonExcludeSensitiveIncludesBuiltIn, containsString(ServiceDocument.FIELD_NAME_VERSION)); String jsonIncludeSensitiveExcludeBuiltIn = Utils.toJson(EnumSet.of(JsonMapper.JsonOptions.EXCLUDE_BUILTIN), testDoc); assertThat(jsonIncludeSensitiveExcludeBuiltIn, containsString("complexA")); assertThat(jsonIncludeSensitiveExcludeBuiltIn, containsString("sensitive")); assertThat(jsonIncludeSensitiveExcludeBuiltIn, not(containsString(ServiceDocument.FIELD_NAME_VERSION))); String jsonExcludeSensitiveAndBuiltInCompact = Utils.toJson( EnumSet.of(JsonMapper.JsonOptions.EXCLUDE_SENSITIVE, JsonMapper.JsonOptions.EXCLUDE_BUILTIN, JsonMapper.JsonOptions.COMPACT), testDoc); assertThat(jsonExcludeSensitiveAndBuiltInCompact, containsString("complexA")); assertThat(jsonExcludeSensitiveAndBuiltInCompact, not(containsString("sensitive"))); assertThat(jsonExcludeSensitiveAndBuiltInCompact, not(containsString(ServiceDocument.FIELD_NAME_VERSION))); assertFalse("compact JSON should not have whitespaces", containsWhitespacePattern.matcher(jsonExcludeSensitiveAndBuiltInCompact).find()); }