/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.ilmlf.sitetositeconnection; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.PrettyPrinter; import com.fasterxml.jackson.core.util.DefaultIndenter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.core.util.Separators; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import java.io.UncheckedIOException; public final class Snapshot { private static final ObjectMapper objectMapper = buildObjectMapper(); private static final PrettyPrinter pp = buildDefaultPrettyPrinter(); /** * Workaround for an incompatibility between latest Jackson and json-snapshot libs. *

* Intended to replace {@code io.github.jsonSnapshot.SnapshotMatcher#defaultJsonFunction} * * @see Issue in json-snapshot project */ public static String asJsonString(Object object) { try { return objectMapper.writer(pp).writeValueAsString(object); } catch (JsonProcessingException e) { throw new UncheckedIOException(e); } } /** * Unmodified copy of {@code io.github.jsonSnapshot.SnapshotMatcher#buildObjectMapper} */ private static ObjectMapper buildObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); objectMapper.setVisibility( objectMapper .getSerializationConfig() .getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); return objectMapper; } /** * Modified copy of {@code io.github.jsonSnapshot.SnapshotMatcher#buildDefaultPrettyPrinter} */ private static PrettyPrinter buildDefaultPrettyPrinter() { DefaultPrettyPrinter pp = new DefaultPrettyPrinter("") { @Override public DefaultPrettyPrinter createInstance() { return this; } @Override public DefaultPrettyPrinter withSeparators(Separators separators) { this._separators = separators; this._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " "; return this; } }; DefaultPrettyPrinter.Indenter lfOnlyIndenter = new DefaultIndenter(" ", "\n"); pp.indentArraysWith(lfOnlyIndenter); pp.indentObjectsWith(lfOnlyIndenter); return pp; } }