|
| 1 | +package org.wikidata.wdtk.datamodel.helpers; |
| 2 | + |
| 3 | +/*- |
| 4 | + * #%L |
| 5 | + * Wikidata Toolkit Data Model |
| 6 | + * %% |
| 7 | + * Copyright (C) 2014 - 2020 Wikidata Toolkit Developers |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 26 | +import org.wikidata.wdtk.datamodel.implementation.*; |
| 27 | +import org.wikidata.wdtk.datamodel.interfaces.*; |
| 28 | + |
| 29 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 30 | + |
| 31 | +/** |
| 32 | + * Helper to deserialize datamodel objects from their |
| 33 | + * JSON representation. |
| 34 | + * |
| 35 | + * We accept empty arrays as empty maps since there has |
| 36 | + * been a confusion in the past between the two: |
| 37 | + * https://phabricator.wikimedia.org/T138104 |
| 38 | + * |
| 39 | + * @author Antonin Delpeuch |
| 40 | + */ |
| 41 | +public class JsonDeserializer { |
| 42 | + |
| 43 | + private ObjectReader entityDocumentReader; |
| 44 | + private ObjectReader itemReader; |
| 45 | + private ObjectReader propertyReader; |
| 46 | + private ObjectReader lexemeReader; |
| 47 | + private ObjectReader mediaInfoReader; |
| 48 | + private ObjectReader entityRedirectReader; |
| 49 | + |
| 50 | + /** |
| 51 | + * Constructs a new JSON deserializer for the |
| 52 | + * designated site. |
| 53 | + * |
| 54 | + * @param siteIri |
| 55 | + * Root IRI of the site to deserialize for |
| 56 | + */ |
| 57 | + public JsonDeserializer(String siteIri) { |
| 58 | + DatamodelMapper mapper = new DatamodelMapper(siteIri); |
| 59 | + entityDocumentReader = mapper.readerFor(EntityDocumentImpl.class) |
| 60 | + .with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); |
| 61 | + itemReader = mapper.readerFor(ItemDocumentImpl.class) |
| 62 | + .with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); |
| 63 | + propertyReader = mapper.readerFor(PropertyDocumentImpl.class) |
| 64 | + .with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); |
| 65 | + lexemeReader = mapper.readerFor(LexemeDocumentImpl.class) |
| 66 | + .with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); |
| 67 | + mediaInfoReader = mapper.readerFor(MediaInfoDocumentImpl.class) |
| 68 | + .with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); |
| 69 | + entityRedirectReader = mapper.readerFor(EntityRedirectDocumentImpl.class) |
| 70 | + .with(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Deserializes a JSON string into an {@class ItemDocument}. |
| 75 | + * @throws IOException |
| 76 | + if the JSON payload is invalid |
| 77 | + */ |
| 78 | + public ItemDocument deserializeItemDocument(String json) throws IOException { |
| 79 | + return itemReader.readValue(json); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Deserializes a JSON string into a {@class PropertyDocument}. |
| 84 | + * @throws IOException |
| 85 | + if the JSON payload is invalid |
| 86 | + */ |
| 87 | + public PropertyDocument deserializePropertyDocument(String json) throws IOException { |
| 88 | + return propertyReader.readValue(json); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Deserializes a JSON string into a {@class LexemeDocument}. |
| 93 | + * @throws IOException |
| 94 | + if the JSON payload is invalid |
| 95 | + */ |
| 96 | + public LexemeDocument deserializeLexemeDocument(String json) throws IOException { |
| 97 | + return lexemeReader.readValue(json); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Deserializes a JSON string into a {@class MediaInfoDocument}. |
| 102 | + * @throws IOException |
| 103 | + if the JSON payload is invalid |
| 104 | + */ |
| 105 | + public MediaInfoDocument deserializeMediaInfoDocument(String json) throws IOException { |
| 106 | + return mediaInfoReader.readValue(json); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Deserializes a JSON string into a {@class EntityDocument}. |
| 111 | + * @throws IOException |
| 112 | + if the JSON payload is invalid |
| 113 | + */ |
| 114 | + public EntityDocument deserializeEntityDocument(String json) throws IOException { |
| 115 | + return entityDocumentReader.readValue(json); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Deserializes a JSON string into a {@class EntityRedirectDocument}. |
| 120 | + * @throws IOException |
| 121 | + if the JSON payload is invalid |
| 122 | + */ |
| 123 | + public EntityRedirectDocument deserializeEntityRedirectDocument(String json) throws IOException { |
| 124 | + return entityRedirectReader.readValue(json); |
| 125 | + } |
| 126 | +} |
0 commit comments