|
1 | 1 | package io.github.jsonSnapshot;
|
2 | 2 |
|
3 |
| -import com.google.gson.GsonBuilder; |
| 3 | +import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| 4 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 5 | +import com.fasterxml.jackson.core.util.DefaultIndenter; |
| 6 | +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; |
| 7 | +import com.fasterxml.jackson.core.util.Separators; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.SerializationFeature; |
4 | 10 | import org.apache.commons.lang3.ArrayUtils;
|
5 | 11 | import org.apache.commons.lang3.StringUtils;
|
6 | 12 | import org.assertj.core.util.Arrays;
|
@@ -89,8 +95,45 @@ public static Snapshot expect(Object firstObject, Object... others) {
|
89 | 95 | }
|
90 | 96 | }
|
91 | 97 |
|
92 |
| - private static Function<Object, String> defaultJsonFunction() { |
93 |
| - return (object) -> new GsonBuilder().setPrettyPrinting().create().toJson(object); |
| 98 | + static Function<Object, String> defaultJsonFunction() { |
| 99 | + |
| 100 | + ObjectMapper objectMapper = buildObjectMapper(); |
| 101 | + |
| 102 | + DefaultPrettyPrinter pp = buildDefaultPrettyPrinter(); |
| 103 | + |
| 104 | + return (object) -> { |
| 105 | + try { |
| 106 | + return objectMapper.writer(pp).writeValueAsString(object); |
| 107 | + } catch (Exception e) { |
| 108 | + throw new SnapshotMatchException(e.getMessage()); |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + private static DefaultPrettyPrinter buildDefaultPrettyPrinter() { |
| 114 | + DefaultPrettyPrinter pp = new DefaultPrettyPrinter(""){ |
| 115 | + @Override |
| 116 | + public DefaultPrettyPrinter withSeparators(Separators separators) { |
| 117 | + this._separators = separators; |
| 118 | + this._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " "; |
| 119 | + return this; |
| 120 | + } |
| 121 | + }; |
| 122 | + pp.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE); |
| 123 | + return pp; |
| 124 | + } |
| 125 | + |
| 126 | + private static ObjectMapper buildObjectMapper() { |
| 127 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 128 | + objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); |
| 129 | + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 130 | + |
| 131 | + objectMapper.setVisibility(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() |
| 132 | + .withFieldVisibility(JsonAutoDetect.Visibility.ANY) |
| 133 | + .withGetterVisibility(JsonAutoDetect.Visibility.NONE) |
| 134 | + .withSetterVisibility(JsonAutoDetect.Visibility.NONE) |
| 135 | + .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); |
| 136 | + return objectMapper; |
94 | 137 | }
|
95 | 138 |
|
96 | 139 | private static void validateExpectCall(Snapshot snapshot) {
|
|
0 commit comments