Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-cherednik committed Feb 18, 2024
1 parent 4be644d commit c0a7a0a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/main/java/ru/olegcherednik/json/impl/JacksonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ private static ObjectMapper config(ObjectMapper mapper, JsonSettings settings) {
mapper.setTimeZone(TimeZone.getTimeZone(settings.getZoneId()));

mapper.getSerializerProvider().setNullKeySerializer(JacksonNullKeySerializer.INSTANCE);
mapper.configOverride(Map.class).setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_NULL,
JsonInclude.Include.ALWAYS));

JsonInclude.Include contentIncl = settings.isSerializeNullMapValue() ? JsonInclude.Include.ALWAYS
: JsonInclude.Include.NON_NULL;
mapper.configOverride(Map.class)
.setInclude(JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, contentIncl));

return mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE)
.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Oleg Cherednik
*
* 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 ru.olegcherednik.json.jackson.enumid;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import ru.olegcherednik.json.api.enumid.EnumId;

import java.io.IOException;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
final class EnumIdKeySerializer extends JsonSerializer<EnumId> {

public static final EnumIdKeySerializer INSTANCE = new EnumIdKeySerializer();

@Override
public void serialize(EnumId enumId, JsonGenerator generator, SerializerProvider serializers) throws IOException {
generator.writeFieldName(enumId.getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class EnumIdModule extends SimpleModule {

public EnumIdModule() {
addSerializer(EnumId.class, EnumIdSerializer.INSTANCE);
addKeySerializer(EnumId.class, EnumIdKeySerializer.INSTANCE);
_deserializers = EnumIdDeserializers.INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

package ru.olegcherednik.json.jackson;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.testng.annotations.Test;
import ru.olegcherednik.json.api.Json;
import ru.olegcherednik.json.api.JsonSettings;
import ru.olegcherednik.json.api.enumid.EnumId;

import java.util.Map;

Expand All @@ -30,16 +35,60 @@
@Test
public class MapNullKeyValueTest {

private static final String ONE = "one";

public void shouldSerializeNulWhenWriteMap() {
Map<String, String> expected = MapUtils.of("one", "1",
Map<String, String> expected = MapUtils.of(ONE, "1",
"two", null);
String json = Json.writeValue(expected);
assertThat(json).isNotNull();
assertThat(json).isEqualTo("{\"one\":\"1\",\"two\":null}");
assertThat(json).isEqualTo("{\"" + ONE + "\":\"1\",\"two\":null}");

Map<String, String> actual = Json.readMap(json, String.class, String.class);
assertThat(actual).isEqualTo(expected);
}

public void shouldNotSerializeNulWhenWriteMapWithCustomSetting() {
JsonSettings settings = JsonSettings.builder()
.serializeNullMapValue(false)
.build();

Map<String, String> expected = MapUtils.of(ONE, "1",
"two", null);
String json = Json.createWriter(settings).writeValue(expected);
assertThat(json).isNotNull();
assertThat(json).isEqualTo("{\"" + ONE + "\":\"1\"}");

Map<String, String> actual = Json.readMap(json, String.class, String.class);
assertThat(actual).containsOnlyKeys(ONE)
.containsEntry(ONE, "1");
}

public void shouldSerializeNulWhenWriteMapWithEnumKey() {
Map<Auto, String> expected = MapUtils.of(Auto.AUDI, "RS3",
Auto.MERCEDES, "G64",
Auto.BMW, null);
String json = Json.writeValue(expected);
assertThat(json).isNotNull();
assertThat(json).isEqualTo("{\"audi\":\"RS3\",\"mercedes\":\"G64\",\"bmw\":null}");

Map<Auto, String> actual = Json.readMap(json, Auto.class, String.class);
assertThat(actual).isEqualTo(expected);
}

@Getter
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public enum Auto implements EnumId {

AUDI("audi"),
BMW("bmw"),
MERCEDES("mercedes");

private final String id;

public static Auto parseId(String id) {
return EnumId.parseId(Auto.class, id);
}
}

}

0 comments on commit c0a7a0a

Please sign in to comment.