Skip to content

Commit

Permalink
[#10114] Add Jackson Util
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jul 7, 2023
1 parent f9aa761 commit b2fbe4a
Show file tree
Hide file tree
Showing 37 changed files with 232 additions and 77 deletions.
4 changes: 4 additions & 0 deletions commons-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2023 NAVER Corp.
*
* 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.navercorp.pinpoint.common.server.util.json;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.beans.BeanUtils;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.util.ClassUtils;

import java.lang.reflect.Constructor;

public final class Jackson {

private static final Module parameterNamesModule = getParameterNamesModule();

private Jackson() {
}

@SuppressWarnings("unchecked")
private static Module getParameterNamesModule() {
try {
Class<? extends Module> parameterNamesModuleClass = (Class<? extends Module>)
ClassUtils.forName("com.fasterxml.jackson.module.paramnames.ParameterNamesModule", Jackson.class.getClassLoader());
Constructor<? extends Module> constructor = parameterNamesModuleClass.getConstructor(JsonCreator.Mode.class);
return BeanUtils.instantiateClass(constructor, JsonCreator.Mode.DEFAULT);

Check warning on line 44 in commons-server/src/main/java/com/navercorp/pinpoint/common/server/util/json/Jackson.java

View check run for this annotation

Codecov / codecov/patch

commons-server/src/main/java/com/navercorp/pinpoint/common/server/util/json/Jackson.java#L42-L44

Added lines #L42 - L44 were not covered by tests
} catch (ReflectiveOperationException ignore) {
return null;
}
}

public static ObjectMapper newMapper() {
Jackson2ObjectMapperBuilder builder = newBuilder();
return builder.build();
}

public static Jackson2ObjectMapperBuilder newBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
if (parameterNamesModule != null ) {
builder.modulesToInstall(parameterNamesModule);

Check warning on line 58 in commons-server/src/main/java/com/navercorp/pinpoint/common/server/util/json/Jackson.java

View check run for this annotation

Codecov / codecov/patch

commons-server/src/main/java/com/navercorp/pinpoint/common/server/util/json/Jackson.java#L58

Added line #L58 was not covered by tests
}
builder.featuresToDisable(
SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS
);
return builder;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2023 NAVER Corp.
*
* 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.navercorp.pinpoint.common.server.util.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Objects;

class JacksonTest {

@Test
void newMapper() throws JsonProcessingException {
ObjectMapper mapper = Jackson.newMapper();

User user1 = new User();
user1.setName("abc");

String json = mapper.writeValueAsString(user1);

User user2 = mapper.readValue(json, User.class);

Assertions.assertThat(user1).isEqualTo(user2);
}

static class User {
private String name;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

User user = (User) o;

return Objects.equals(name, user.name);
}

@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}
}
4 changes: 4 additions & 0 deletions inspector-module/inspector-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<artifactId>pinpoint-commons</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-commons-server</artifactId>
</dependency>
<dependency>
<groupId>com.navercorp.pinpoint</groupId>
<artifactId>pinpoint-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,37 @@

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.web.util.TagUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
import org.apache.pinot.common.utils.PinotDataType;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author minwoo.jung
*/
public class MultiValueTagTypeHandler implements TypeHandler<List<Tag>> {

private final static ObjectMapper OBJECT_MAPPER;
private final static ObjectMapper OBJECT_MAPPER = getMapper();
private final TypeReference<List<Tag>> REF_LIST_TAG = new TypeReference<>() {

Check warning on line 45 in inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java

View check run for this annotation

Codecov / codecov/patch

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java#L44-L45

Added lines #L44 - L45 were not covered by tests
};

static {
OBJECT_MAPPER = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(List.class, new CustomObjectListDeserializer());
OBJECT_MAPPER.registerModule(module);
private static ObjectMapper getMapper() {
return Jackson.newBuilder()
.deserializerByType(List.class, new CustomObjectListDeserializer())
.build();

Check warning on line 51 in inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java

View check run for this annotation

Codecov / codecov/patch

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java#L49-L51

Added lines #L49 - L51 were not covered by tests
}

@Override
Expand All @@ -65,8 +61,7 @@ public List<Tag> getResult(ResultSet rs, String columnName) throws SQLException
String jsonString = rs.getString(columnName);

try {
List<Tag> tagList = OBJECT_MAPPER.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class, Tag.class));
return tagList;
return OBJECT_MAPPER.readValue(jsonString, REF_LIST_TAG);

Check warning on line 64 in inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java

View check run for this annotation

Codecov / codecov/patch

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java#L64

Added line #L64 was not covered by tests
} catch (JsonProcessingException e) {
throw new SQLException(e);
}
Expand All @@ -87,10 +82,14 @@ private List<Tag> createTagList(Object tagValues) {
}

static class CustomObjectListDeserializer extends JsonDeserializer<List<Tag>> {

private static final TypeReference<List<String>> REF = new TypeReference<>() {

Check warning on line 86 in inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java

View check run for this annotation

Codecov / codecov/patch

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java#L86

Added line #L86 was not covered by tests
};

@Override
public List<Tag> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
List<String> list = jsonParser.readValueAs(ArrayList.class);

List<String> list = jsonParser.readValueAs(REF);

Check warning on line 92 in inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java

View check run for this annotation

Codecov / codecov/patch

inspector-module/inspector-web/src/main/java/com/navercorp/pinpoint/inspector/web/dao/pinot/MultiValueTagTypeHandler.java#L92

Added line #L92 was not covered by tests
List<Tag> tagList = new ArrayList<>();
for (String tagString : list) {
Tag tag = TagUtils.parseTag(tagString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.navercorp.pinpoint.metric.common.model.mybatis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.metric.common.model.json.Tags;
import org.apache.ibatis.type.JdbcType;
Expand All @@ -37,19 +37,16 @@
* @author minwoo.jung
*/
@MappedJdbcTypes({JdbcType.VARCHAR})
public class TagListTypeHandler implements TypeHandler<List<Tag>> {
public class TagListTypeHandler implements TypeHandler<List<Tag>> {

Check warning on line 40 in metric-module/metric/src/main/java/com/navercorp/pinpoint/metric/common/model/mybatis/TagListTypeHandler.java

View check run for this annotation

Codecov / codecov/patch

metric-module/metric/src/main/java/com/navercorp/pinpoint/metric/common/model/mybatis/TagListTypeHandler.java#L40

Added line #L40 was not covered by tests

private final Logger logger = LogManager.getLogger(this.getClass());
private final static ObjectMapper OBJECT_MAPPER = createObjectMapper();
private final static ObjectMapper OBJECT_MAPPER = getMapper();


public static ObjectMapper createObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();

SimpleModule module = new SimpleModule();
Class<List<Tag>> type = ((Class) List.class);
module.addSerializer(type, new TagMySqlSerializer());
return objectMapper.registerModule(module);
static ObjectMapper getMapper() {
return Jackson.newBuilder()
.serializerByType(List.class, new TagMySqlSerializer())
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.navercorp.pinpoint.metric.collector.model.serialize;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.metric.collector.model.TelegrafMetric;
import com.navercorp.pinpoint.metric.collector.model.TelegrafMetrics;
import org.apache.logging.log4j.LogManager;
Expand All @@ -17,7 +18,7 @@
public class TelegrafJsonDeserializerTest {
private final Logger logger = LogManager.getLogger(this.getClass());

private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = Jackson.newMapper();


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.metric.common.model.Tag;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.metric.collector.view.SystemMetricView;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -33,7 +33,7 @@
public class SystemMetricSerializerTest {

private final Logger logger = LogManager.getLogger(this.getClass());
private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = Jackson.newMapper();

@Test
public void testLongCounterWithTags() throws JsonProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TagListTypeHandlerTest {

@Test
public void test() throws JsonProcessingException {
ObjectMapper mapper = TagListTypeHandler.createObjectMapper();
ObjectMapper mapper = TagListTypeHandler.getMapper();

List<Tag> list = List.of(
new Tag("a", "1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.common.server.util.json.TypeRef;
import com.navercorp.pinpoint.common.util.StringUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.Map;
Expand All @@ -40,7 +41,7 @@ public class PinpointWebSocketMessageConverter {

private static final Logger LOGGER = LogManager.getLogger(PinpointWebSocketMessageConverter.class);

private static final ObjectMapper JSON_SERIALIZER = new ObjectMapper();
private static final ObjectMapper JSON_SERIALIZER = Jackson.newMapper();

private final String pingMessage;
private final String pongMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.navercorp.pinpoint.web.applicationmap;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.web.applicationmap.link.LinkList;
import com.navercorp.pinpoint.web.applicationmap.nodes.NodeList;
Expand All @@ -33,19 +33,12 @@
public class ApplicationMapTest {
private final Logger logger = LogManager.getLogger(this.getClass());

private final ObjectMapper MAPPER = newObjectMapper();

private ObjectMapper newObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
return mapper;
}

private final ObjectMapper mapper = Jackson.newMapper();

@Test
public void root() throws IOException {
ApplicationMap app = new DefaultApplicationMap(Range.between(0, 1), new NodeList(), new LinkList());
String s = MAPPER.writeValueAsString(app);
String s = mapper.writeValueAsString(app);
logger.debug(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.navercorp.pinpoint.common.server.util.json.Jackson;
import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.util.TimeWindow;
Expand Down Expand Up @@ -46,7 +47,7 @@ public class AgentTimeHistogramTest {

private final Logger logger = LogManager.getLogger(this.getClass());

private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper = Jackson.newMapper();

@Test
public void testViewModel() throws IOException {
Expand Down
Loading

0 comments on commit b2fbe4a

Please sign in to comment.