|
21 | 21 | import java.nio.CharBuffer;
|
22 | 22 | import java.nio.charset.Charset;
|
23 | 23 | import org.json.JSONException;
|
| 24 | +import org.json.JSONObject; |
24 | 25 |
|
25 | 26 | /**
|
26 | 27 | * Reads the length-prefixed JSON stream for Bundles.
|
@@ -101,7 +102,7 @@ private BundleElement readNextElement() throws IOException, JSONException {
|
101 | 102 |
|
102 | 103 | String json = readJsonString(Integer.parseInt(lengthPrefix));
|
103 | 104 | bytesRead += lengthPrefix.length() + json.length();
|
104 |
| - return BundleElement.fromJson(serializer, json); |
| 105 | + return decodeBundleElement(json); |
105 | 106 | }
|
106 | 107 |
|
107 | 108 | /**
|
@@ -190,6 +191,23 @@ private boolean pullMoreData() throws IOException {
|
190 | 191 | return buffer.remaining() > 0;
|
191 | 192 | }
|
192 | 193 |
|
| 194 | + /** Converts a JSON-encoded bundle element into its model class. */ |
| 195 | + private BundleElement decodeBundleElement(String json) throws JSONException { |
| 196 | + JSONObject object = new JSONObject(json); |
| 197 | + |
| 198 | + if (object.has("metadata")) { |
| 199 | + return serializer.decodeBundleMetadata(object.getJSONObject("metadata")); |
| 200 | + } else if (object.has("namedQuery")) { |
| 201 | + return serializer.decodeNamedQuery(object.getJSONObject("namedQuery")); |
| 202 | + } else if (object.has("documentMetadata")) { |
| 203 | + return serializer.decodeBundledDocumentMetadata(object.getJSONObject("documentMetadata")); |
| 204 | + } else if (object.has("document")) { |
| 205 | + return serializer.decodeDocument(object.getJSONObject("document")); |
| 206 | + } else { |
| 207 | + throw new IllegalArgumentException("Cannot decode unknown Bundle element: " + json); |
| 208 | + } |
| 209 | + } |
| 210 | + |
193 | 211 | /** Closes the underlying stream and raises an IllegalArgumentException. */
|
194 | 212 | private void raiseError(String message) throws IOException {
|
195 | 213 | close();
|
|
0 commit comments