1

我正在尝试根据运行时给出的规范在 java 中实现 JSON 到 JSON 的转换。

示例:如果在运行时 source : com.gsdetails.gname,target : com.track.trackName (即源字段应映射到生成的 JSON 中的目标字段)

我的方法是为规范部分创建 N 数组树并进行广度优先遍历(使用它获取队列以创建结果 json 的结构)我正在使用 Jackson api 从输入 JSON 创建树并遍历队列(bfs)和输入树来创建结果 json。

无法获得预期的输出

PS:我想过使用 JOLT api,但它不符合我的目的

树(用于规范)


public class TrieBuilder {

public static void main(String[] args) throws Exception {
    createSpec();
}

public static Trie createSpec() throws Exception {
    BufferedReader reader = new BufferedReader(
            new FileReader(""));
    String currentLine = reader.readLine();
    Trie trie = new Trie();
    while (currentLine != null) {
        String[] lines = currentLine.split(",");
        String sourceLine = lines[0];
        String targetLine = lines[1];
        String sourcePath = sourceLine.split("=")[1];
        String targetPath = targetLine.split("=")[1];

        trie.insertWord(sourcePath.trim(), targetPath.trim());

        currentLine = reader.readLine();
    }
    return trie;
}

}

class TrieNode { String source;// consider this as content/reference point of a tree String target; boolean isEnd; int count; List childList; boolean isRoot;

/* Constructor */
public TrieNode(String source, String target) {
    childList = new ArrayList<TrieNode>();
    isEnd = false;
    count = 0;
    this.source = source;
    this.target = target;
}

public TrieNode subNodeWord(String word) {
    if (childList != null) {
        for (TrieNode eachChild : childList)
            if (eachChild.source.equals(word))
                return eachChild;
    }
    return null;
}

}

class Trie { public TrieNode root;

/* Constructor */
public Trie() {
    root = new TrieNode("", "");
}

public void insertWord(String sourceWords, String targetWords) {
    if (searchWord(sourceWords) == true)
        return;
    TrieNode current = root;

    String[] sourceArray = sourceWords.split(":");
    String[] targetArray = targetWords.split(":");

    for (int i = 0; i < sourceArray.length; i++) {
        TrieNode child = current.subNodeWord(sourceArray[i]);
        if (child != null) {
            current = child;
        } else {
            current.childList.add(new TrieNode(sourceArray[i],
                    targetArray[i]));
            current = current.subNodeWord(sourceArray[i]);
        }
        current.count++;
    }
    current.isEnd = true;
}

public boolean searchWord(String words) {
    TrieNode current = root;
    for (String word : words.split(":")) {
        if (current.subNodeWord(word) == null) {
            return false;
        } else {
            current = current.subNodeWord(word);
        }
    }

    if (current.isEnd == true)
        return true;
    return false;
}

public Queue<TrieNode> bfsTraversal(TrieNode node) {
    // TODO need to add logic for bfs/dfs for traversing the trie
    Queue<TrieNode> queue = new LinkedList<>();
    Queue<TrieNode> tempQueue = new LinkedList<>();
    queue.add(root);
    while (!queue.isEmpty()) {
        TrieNode tempNode = queue.poll();
        tempQueue.add(tempNode);
        int counter = tempNode.childList.size(), i = 0;
        if (tempNode == null)
            break;
        if (!tempNode.source.isEmpty())
            System.out.println("Source :" + tempNode.source
                    + "     Target : " + tempNode.target);
        while (i < counter) {
            queue.add(tempNode.childList.get(i++));
        }
    }
    tempQueue.poll();
    return tempQueue;
}

源到目标映射文件:


    source = com:track:trackDetails:fname, target = gsUser:gsProp:gsDetails:gsFirstName
    source = com:track:trackDetails:lname, target = gsUser:gsProp:gsDetails:gsLastName

助手类(实际转换):


public class JsonHelperClass{

//  private Files file = null;// create a tempfile
    private JsonNodeFactory factory;
    private JsonFactory jsonFactory;
    private ObjectMapper mapper;
    private JsonNode jsonRoot;
    private Queue<TrieNode> queue;
//  private JsonParser jsonParser =  

    public JsonHelperClass() throws JsonProcessingException, IOException {
        this.factory = JsonNodeFactory.instance;
        this.jsonFactory  = new JsonFactory();
        this.mapper  = new ObjectMapper();
        this.jsonRoot = mapper.readTree(new File("json with data"));
    }

    public static void main(String[] args) throws Exception, Exception {
            JsonHelperClass helperClass = new JsonHelperClass();
            helperClass.jsonCreator();
            ObjectNode objectNode = null; 
            ObjectNode result = helperClass.createJsonRecursively(objectNode);
            System.out.println(result.toString());

    }

    public void jsonCreator() throws Exception {
        Trie trie = TrieBuilder.createSpec();
        queue = trie.bfsTraversal(trie.root);

    }

    public ObjectNode createJsonRecursively(ObjectNode outputJson) throws Exception {
        TrieNode nodeOfQueue = queue.poll();

        if(outputJson == null){
            // create a root of the JSON
            outputJson = factory.objectNode();
            outputJson.put(nodeOfQueue.target, createJsonRecursively(outputJson));


        }else if (jsonRoot.get(nodeOfQueue.source).isObject()){
            // create an object to conatin other values/object
            ObjectNode objectNode = factory.objectNode();
            objectNode.put(nodeOfQueue.target,createJsonRecursively(outputJson));
            outputJson.putAll(objectNode);


        }else if(jsonRoot.get(nodeOfQueue.source).isArray()){
            // create an array node and call for to create value it contains
            ArrayNode arrayNode = factory.arrayNode();
            int size = jsonRoot.get(nodeOfQueue.source).size();
            for(int index = 0 ; index < size ; index++){
                arrayNode.add(jsonRoot.get(nodeOfQueue.source).get(index));
            }
            outputJson.put(nodeOfQueue.target,arrayNode);           
        }else if(nodeOfQueue.isEnd){
            // create leaf node
            outputJson.put(nodeOfQueue.target, jsonRoot.get(nodeOfQueue.source));
            return outputJson;

        }
        return outputJson;
    }

4

0 回答 0