从Java的一个非常复杂的JSON模式构建JSON

发布于 2025-02-05 07:16:29 字数 4852 浏览 0 评论 0原文

我在这里有一个复杂的问题,对一些建议或建议将不胜感激。从本质上讲,我有一个复杂的JSON模式,看起来像这样:

{
    "$schema": "http://example.org",
    "$id": "http://example.org",
    "title": "schema title",
    "description": "description",
    "properties": {
        "name": {
            "description": "description",
            "type": "string",
            "enum": [
                "name1",
                "name2"
            ]
        },
        "storage": {
            "description": "description",
            "type": "integer",
            "minimum": "200",
            "maximum": "500",
            "default": "200",
        },
        "domain": {
            "description": "description",
            "type": "string"
        },
    },
    "if": {
        "properties": {
            "name": {
                "const": "name1"
            }
        }
    },
    "then": {
        "if": {
            "properties": {
                "version": {
                    "const": "version1"
                }
            }
        },
        "then": {
            "properties": {
                "cpus": {
                    "description": "description",
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 8
                },
                "memory": {
                    "description": "description",
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 32
                },
            },
            "required": [
                "cpus",
                "memory"
            ]
        },
        "else": {
            "if": {
                "properties": {
                    "version": {
                        "const": "version2"
                    }
                }
            },
            "then": {
                "properties": {
                    "cpus": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 8
                    },
                    "diskSize": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 250,
                        "maximum": 1000
                    },
                },
                "required": [
                    "cpus",
                    "diskSize"
                ]
            }
        }
    },
    "else": {
        "if": {
            "properties": {
                "name": {
                    "const": "name2"
                }
            }
        },
        "then": {
            "if": {
                "properties": {
                    "version": {
                        "const": "version3"
                    }
                }
            },
            "then": {
                "properties": {
                    "diskSize": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 100,
                        "maximum": 500
                    }
                    "memory": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 28
                    }
                },
                "required": [
                    "diskSize",
                    "memory"
                ]
            },
            "else": {
                "if": {
                    "properties": {
                        "version": {
                            "const": "version4"
                        }
                    }
                },
                "then": {
                    "properties": {
                        "cpus": {
                            "description": "description",
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 28
                        },
                        "memory": {
                            "description": "description",
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 64
                        }
                    },
                    "required": [
                        "cpus",
                        "memory"
                    ]
                }
            }
        }
    }
}

我需要在Java中使用此架构构建JSON对象。模式中的每个属性都在我可以访问的地图内部,因此我只需从地图中获取属性并将其添加到我正在构建的JSONNODE对象中。初始“属性”对象下的每个属性都很容易检索,我只能获取它们的列表,然后从地图中获取每个属性。

复杂性在于if/then/否则JSON模式的部分。我看到我需要的唯一方法是首先从第一个“属性”对象构建JSON的初始部分,然后具有某种相当复杂的递归算法,该算法进入每个IF/then/then/then/then/else语句和比较要评估的属性的价值,然后返回我需要从地图获得的属性列表。我已经在网上环顾了一个可以从Java中的JSON模式构建JSON的库,但没有找到任何可以处理复杂的If/then/then/else语句的库。

任何建议或想法将不胜感激。

I have a complex issue here and some advice or suggestions would be greatly appreciated. Essentially I have a complex JSON schema that looks something like this:

{
    "$schema": "http://example.org",
    "$id": "http://example.org",
    "title": "schema title",
    "description": "description",
    "properties": {
        "name": {
            "description": "description",
            "type": "string",
            "enum": [
                "name1",
                "name2"
            ]
        },
        "storage": {
            "description": "description",
            "type": "integer",
            "minimum": "200",
            "maximum": "500",
            "default": "200",
        },
        "domain": {
            "description": "description",
            "type": "string"
        },
    },
    "if": {
        "properties": {
            "name": {
                "const": "name1"
            }
        }
    },
    "then": {
        "if": {
            "properties": {
                "version": {
                    "const": "version1"
                }
            }
        },
        "then": {
            "properties": {
                "cpus": {
                    "description": "description",
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 8
                },
                "memory": {
                    "description": "description",
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 32
                },
            },
            "required": [
                "cpus",
                "memory"
            ]
        },
        "else": {
            "if": {
                "properties": {
                    "version": {
                        "const": "version2"
                    }
                }
            },
            "then": {
                "properties": {
                    "cpus": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 8
                    },
                    "diskSize": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 250,
                        "maximum": 1000
                    },
                },
                "required": [
                    "cpus",
                    "diskSize"
                ]
            }
        }
    },
    "else": {
        "if": {
            "properties": {
                "name": {
                    "const": "name2"
                }
            }
        },
        "then": {
            "if": {
                "properties": {
                    "version": {
                        "const": "version3"
                    }
                }
            },
            "then": {
                "properties": {
                    "diskSize": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 100,
                        "maximum": 500
                    }
                    "memory": {
                        "description": "description",
                        "type": "integer",
                        "minimum": 1,
                        "maximum": 28
                    }
                },
                "required": [
                    "diskSize",
                    "memory"
                ]
            },
            "else": {
                "if": {
                    "properties": {
                        "version": {
                            "const": "version4"
                        }
                    }
                },
                "then": {
                    "properties": {
                        "cpus": {
                            "description": "description",
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 28
                        },
                        "memory": {
                            "description": "description",
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 64
                        }
                    },
                    "required": [
                        "cpus",
                        "memory"
                    ]
                }
            }
        }
    }
}

I need to build a JSON object using this schema in java. Every property in the schema is inside of a map that I have access to, so I can quite simply just get the property from the map and add it to a JsonNode object that I am building. Every property under the initial "properties" object is easy to retrieve, I can just get a list of them and then get each one from the map.

The complexity lies in the if/then/else part of the json schema. The only way I can see to find which property I need is to first build the initial part of the json from the first "properties" object and then have some sort of quite complex recursive algorithm that goes into every if/then/else statement and compares the value of the property being evaluated and then returns a list of the properties I need to get from the map. I have looked around online for a library that can build Json from a Json schema in java but haven't found anything that can deal with the complex if/then/else statements.

Any suggestions or ideas would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文