JSON路径:如何将URN引用转换为本地引用

时间:2018-10-10 16:46:10

标签: java maven jsonschema

使用Maven org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.0.0-alpha2将JSON模式文件转换为Java类时,我遇到与urn引用有关的错误,这些错误无法解决。

这是示例错误消息:

[ERROR] Failed to execute goal org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.0.0-alpha2:generate (default) on project model-reservation: Execution default of goal org.jsonschema2pojo:jsonschema
2pojo-maven-plugin:1.0.0-alpha2:generate failed: Unrecognised URI, can't resolve this: urn:jsonschema:com:lumina:pnr:model:Reference: unknown protocol: urn -> [Help 1]

这是它与$ref元素引用的JSON,这会导致异常:

    "remarkFields": {
      "type": "object",
      "additionalProperties": {
        "type": "array",
        "items": {
          "type": "object",
          "id": "urn:jsonschema:com:lumina:pnr:model:FileFinishingField",
          "properties": {
            "lineNumber": {
              "type": "integer"
            },
            "name": {
              "type": "string"
            },
            "value": {
              "type": "string"
            },
            "references": {
              "type": "array",
              "items": {
                "type": "object",
                "$ref": "urn:jsonschema:com:lumina:pnr:model:Reference"
              }
            }
          }
        }
      }
    }

如何使用org.jsonschema2pojo:jsonschema2pojo-maven-plugin:1.0.0-alpha2 Maven插件将这些转换为Java中的本地引用,并使用转换输出成功地将代码转换为Java类?

1 个答案:

答案 0 :(得分:0)

也许有更好的方法来解决此问题,但一个可能的解决方案是:

使用递归方法将urn样式引用转换为本地引用:

这是使用Jackson图书馆的递归方法:

private static final String ITEMS = "items";
private static final String ID = "id";
private static final String PROPERTIES = "properties";
private static final String ADDITIONAL_PROPERTIES = "additionalProperties";
private static final String REF = "$ref";

private static void parseReferences(JsonNode jsonNode, String path) {
    if (jsonNode.has(ID)) {
        typeMap.put(jsonNode.get(ID).asText(), path);
        final JsonNode properties = jsonNode.get(PROPERTIES);
        final Iterator<Map.Entry<String, JsonNode>> fields = properties.fields();
        path += "/" + PROPERTIES;

        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> entry = fields.next();
            parseReferences(entry.getValue(), path + "/" + entry.getKey());
        }
    } else if (jsonNode.has(ITEMS)) {
        final JsonNode item = jsonNode.get(ITEMS);
        parseReferences(item, path + "/" + ITEMS);
    } else if (jsonNode.has(REF)) {
        ObjectNode objectNode = (ObjectNode) jsonNode;
        objectNode.set(REF, new TextNode(typeMap.get(jsonNode.get(REF).asText())));
    } else if (jsonNode.has(ADDITIONAL_PROPERTIES)) {
        JsonNode additionalProperties = jsonNode.get(ADDITIONAL_PROPERTIES);
        parseReferences(additionalProperties, path + "/" + ADDITIONAL_PROPERTIES);
    }
}

这是实例化JAXB解析器后如何调用此方法:

private static void writeSchemaToFile(ObjectMapper jaxbObjectMapper, String origPath, String path) throws Exception {
    InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(origPath);
    try (Reader r = new InputStreamReader(resourceAsStream, "UTF-8")) {
        JsonNode root = jaxbObjectMapper.readTree(r);
        parseReferences(root, "#");
        String changedJson = jaxbObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
        final Path targetPath = Paths.get(path);
        if (!Files.exists(targetPath)) {
            Path parent = targetPath.getParent();
            if (parent != null) {
                Files.createDirectories(parent);
            }
        }
        try (Writer writer = Files.newBufferedWriter(targetPath, Charset.forName("UTF-8"), StandardOpenOption.CREATE)) {
            writer.write(changedJson);
        }
    }
}

如果调用此方法,它将把问题中指定的JSON转换为:

"remarkFields" : {
  "type" : "object",
  "additionalProperties" : {
    "type" : "array",
    "items" : {
      "type" : "object",
      "id" : "urn:jsonschema:com:lumina:pnr:model:FileFinishingField",
      "properties" : {
        "lineNumber" : {
          "type" : "integer"
        },
        "name" : {
          "type" : "string"
        },
        "value" : {
          "type" : "string"
        },
        "references" : {
          "type" : "array",
          "items" : {
            "type" : "object",
            "$ref" : "#/properties/passengers/items/properties/reference"
          }
        }
      }
    }
  }
}