rmannibucau commented on a change in pull request #39: Deserialization of 
primitives
URL: https://github.com/apache/johnzon/pull/39#discussion_r271667347
 
 

 ##########
 File path: 
johnzon-jsonb/src/main/java/org/apache/johnzon/jsonb/JsonValueParserAdapter.java
 ##########
 @@ -0,0 +1,146 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.johnzon.jsonb;
+
+import java.math.BigDecimal;
+import java.util.EnumSet;
+import java.util.function.Supplier;
+
+import javax.json.JsonArray;
+import javax.json.JsonNumber;
+import javax.json.JsonObject;
+import javax.json.JsonString;
+import javax.json.JsonValue;
+import javax.json.JsonValue.ValueType;
+import javax.json.stream.JsonLocation;
+import javax.json.stream.JsonParser;
+import javax.json.stream.JsonParserFactory;
+
+class JsonValueParserAdapter<T extends JsonValue> implements JsonParser {
+    
+    private static class JsonStringParserAdapter extends 
JsonValueParserAdapter<JsonString> {
+
+        public JsonStringParserAdapter(JsonString jsonValue) {
+            super(jsonValue);
+        }
+        
+        @Override
+        public String getString() {
+            return getValue().getString();
+        }
+    }
+    
+    private static class JsonNumberParserAdapter extends 
JsonValueParserAdapter<JsonNumber> {
+        
+        public JsonNumberParserAdapter(JsonNumber jsonValue) {
+            super(jsonValue);
+        }
+
+        @Override
+        public boolean isIntegralNumber() {
+            return getValue().isIntegral();
+        }
+
+        @Override
+        public int getInt() {
+            return getValue().intValueExact();
+        }
+
+        @Override
+        public long getLong() {
+            return getValue().longValueExact();
+        }
+
+        @Override
+        public BigDecimal getBigDecimal() {
+            return getValue().bigDecimalValue();
+        }
+    }
+    
+    public static JsonParser createFor(JsonValue jsonValue, 
Supplier<JsonParserFactory> parserFactoryProvider) {
+        if (jsonValue instanceof JsonObject) {
+            return parserFactoryProvider.get().createParser((JsonObject) 
jsonValue);
+        } else if (jsonValue instanceof JsonArray) {
+            return parserFactoryProvider.get().createParser((JsonArray) 
jsonValue);
+        } else if (jsonValue instanceof JsonString) {
+            return new JsonStringParserAdapter((JsonString) jsonValue);
+        } else if (jsonValue instanceof JsonNumber) {
+            return new JsonNumberParserAdapter((JsonNumber) jsonValue);
+        } else if (EnumSet.of(ValueType.FALSE, 
ValueType.TRUE).contains(jsonValue.getValueType())) {
+            return new JsonValueParserAdapter<>(jsonValue);
+        }
+        throw new IllegalArgumentException("Cannot create JsonParser for " + 
jsonValue.getValueType());
+    }
+    
+    private final T jsonValue;
+    
+    JsonValueParserAdapter(T jsonValue) {
+        this.jsonValue = jsonValue;
+    }
+
+    @Override
+    public boolean hasNext() {
+        return false;
+    }
+
+    @Override
+    public Event next() {
 
 Review comment:
   makes a lot of unsupported, wonder if the same trick than on the write side 
can be applied, i.e wrapping the actual impl and just having a small event 
buffer to handle primitive/object cases, wdyt? did you estimate it?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to