Skip to content

Commit

Permalink
- B JsonUtils.prettyPrint to not override passed-in GsonBuilder, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed Sep 23, 2024
1 parent aa8e32e commit 888e815
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L291-L299) (Function7, Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L309-L317) (Function8, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* CombinationApprovals. [verifyBestCoveringPairs ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/combinations/CombinationApprovals.java#L326-L333) (Function9, Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], Object[], $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L53-L56) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L48-L51) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L55-L58) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L50-L53) (Object, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L17-L20) (String, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L35-L39) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L34) (String, boolean, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L37-L41) (String, boolean, $\color{#AAA}{\textsf{Options}}$)
* JsonApprovals. [verifyJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonApprovals.java#L29-L36) (String, boolean, Function1, $\color{#AAA}{\textsf{Options}}$)
* JsonJacksonApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonJacksonApprovals.java#L14-L17) (Object, $\color{#AAA}{\textsf{Options}}$)
* JsonXstreamApprovals. [verifyAsJson ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/JsonXstreamApprovals.java#L18-L21) (Object, $\color{#AAA}{\textsf{Options}}$)
* VelocityApprovals. [verify ](https://github.com/approvals/ApprovalTests.Java/blob/master/approvaltests/src/main/java/org/approvaltests/velocity/VelocityApprovals.java#L16-L24) (ContextAware, $\color{#AAA}{\textsf{Options}}$)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "value",
"localDate": null
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.approvaltests;

import com.spun.util.Wrapper;
import com.google.gson.GsonBuilder;
import org.junit.jupiter.api.Test;
import org.lambda.functions.Function1;

import java.time.LocalDateTime;

Expand All @@ -13,6 +14,17 @@ void nullDateTest()
LocalDateWrapper localDateWrapper = new LocalDateWrapper();
JsonApprovals.verifyAsJson(localDateWrapper, g -> g.serializeNulls());
}
@Test
void applesauce()
{
String jsonString = """
{
"name": "value",
"localDate": null
}
""";
JsonApprovals.verifyJson(jsonString, false, GsonBuilder::serializeNulls);
}
private class LocalDateWrapper
{
public LocalDateTime getLocalDate()
Expand Down
15 changes: 12 additions & 3 deletions approvaltests-util/src/main/java/com/spun/util/JsonUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.spun.util;

import com.google.gson.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.lambda.functions.Function1;

import java.io.IOException;
import java.lang.reflect.Field;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Map;
Expand All @@ -16,6 +21,10 @@
public class JsonUtils
{
public static String prettyPrint(String json)
{
return prettyPrint(json, g -> g);
}
public static String prettyPrint(String json, Function1<GsonBuilder, GsonBuilder> gsonBuilder)
{
if (!ObjectUtils.isClassPresent("com.google.gson.Gson"))
{
Expand All @@ -24,7 +33,7 @@ public static String prettyPrint(String json)
}
try
{
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Gson gson = gsonBuilder.call(new GsonBuilder()).setPrettyPrinting().create();
JsonElement je = JsonParser.parseString(json);
return gson.toJson(je);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public static void verifyJson(String json, boolean reorderJson, Function1<GsonBu
public static void verifyJson(String json, boolean reorderJson, Function1<GsonBuilder, GsonBuilder> gsonBuilder,
Options options)
{
String formattedJson = reorderJson ? JsonUtils.reorderFields(json, gsonBuilder) : JsonUtils.prettyPrint(json);
String formattedJson = reorderJson
? JsonUtils.reorderFields(json, gsonBuilder)
: JsonUtils.prettyPrint(json, gsonBuilder);
Approvals.verify(formattedJson, options.forFile().withExtension(".json"));
}
public static void verifyJson(String json, boolean reorderJson, Options options)
Expand Down

0 comments on commit 888e815

Please sign in to comment.