|
| 1 | +/* |
| 2 | + * Copyright 2013 FasterXML. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.fasterxml.jackson.module.jsonSchema.types; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.core.JsonParser; |
| 19 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 20 | +import com.fasterxml.jackson.core.TreeNode; |
| 21 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 22 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 23 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import com.fasterxml.jackson.databind.node.BooleanNode; |
| 26 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | +import com.fasterxml.jackson.module.jsonSchema.JsonSchema; |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +/** |
| 31 | + * |
| 32 | + * @author Ignacio del Valle Alles |
| 33 | + */ |
| 34 | +public class AdditionalPropertiesDeserializer extends JsonDeserializer<ObjectSchema.AdditionalProperties> { |
| 35 | + |
| 36 | + @Override |
| 37 | + public ObjectSchema.AdditionalProperties deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { |
| 38 | + ObjectMapper mapper = (ObjectMapper) jp.getCodec(); |
| 39 | + TreeNode node = mapper.readTree(jp); |
| 40 | + String nodeStr = mapper.writeValueAsString(node); |
| 41 | + if (node instanceof ObjectNode) { |
| 42 | + JsonSchema innerSchema = mapper.readValue(nodeStr, JsonSchema.class); |
| 43 | + return new ObjectSchema.SchemaAdditionalProperties(innerSchema); |
| 44 | + } else if (node instanceof BooleanNode) { |
| 45 | + BooleanNode booleanNode = (BooleanNode) node; |
| 46 | + if (booleanNode.booleanValue()) { |
| 47 | + return null; // "additionalProperties":true is the default |
| 48 | + } else { |
| 49 | + return ObjectSchema.NoAdditionalProperties.instance; |
| 50 | + } |
| 51 | + } else { |
| 52 | + throw new JsonMappingException("additionalProperties nodes can only be of " |
| 53 | + + "type boolean or object: " + nodeStr); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments