Skip to content

Commit

Permalink
add json serialize method
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxiaojiawow committed Jul 20, 2023
1 parent 4f12845 commit 959d271
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.hw.langchain.exception.LangChainException;
import com.sun.jna.platform.win32.Netapi32Util.User;

/**
* @author HamaWhite
Expand All @@ -51,6 +52,14 @@ public static String toJsonStringWithIndent(Object object, int indent) {
}
}

public static <T> T convertFromJsonStr(String jsonStr, Class<T> clazz) {
try {
return OBJECT_MAPPER.readValue(jsonStr, clazz);
} catch (JsonProcessingException e) {
throw new LangChainException("Failed to deserialize json str", e);
}
}

private static DefaultPrettyPrinter getPrettyPrinter(int indent) {
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.indentArraysWith(DefaultPrettyPrinter.FixedSpaceIndenter.instance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.hw.langchain.chains.query.constructor;

import static org.junit.jupiter.api.Assertions.*;

import com.hw.langchain.schema.Document;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;

/**
* @author zhangxiaojia002
* @date 2023/7/20 8:52 下午
**/
class JsonUtilsTest {

@Test
void jsonFormatDocuments(){
Document document = new Document(
"test document content", Map.of("fileName", "test.txt"));
List<Document> documentList = Arrays.asList(document);
String documentStr = documentList.toString();
String documentJson = JsonUtils.toJsonStringWithIndent(documentList, 0);
System.out.println("");
}
}

0 comments on commit 959d271

Please sign in to comment.