这是indexloc提供的服务,不要输入任何密码
Skip to content

jonUtil improvement #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions api/src/main/java/com/theokanning/openai/utils/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
package com.theokanning.openai.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

/**
* @author LiangTao
* @date 2024年05月24 15:02
**/
public class JsonUtil {
private static final ObjectMapper mapper = ObjectMapperHolder.mapper;
private static final ObjectMapper MAPPER = ObjectMapperHolder.MAPPER;

public static ObjectMapper getInstance() {
return mapper;
return MAPPER;
}

public static String writeValueAsString(Object value) {
try {
return mapper.writeValueAsString(value);
return MAPPER.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public static <T> T readValue(String content, Class<T> valueType) {
try {
return mapper.readValue(content, valueType);
return MAPPER.readValue(content, valueType);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private static class ObjectMapperHolder {
private static final ObjectMapper mapper = new ObjectMapper();
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
MAPPER.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE);
}
}


Expand Down