[ 
https://issues.apache.org/jira/browse/PHOENIX-628?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17810289#comment-17810289
 ] 

ASF GitHub Bot commented on PHOENIX-628:
----------------------------------------

ranganathg commented on code in PR #1780:
URL: https://github.com/apache/phoenix/pull/1780#discussion_r1464572192


##########
phoenix-core-client/src/main/java/org/apache/phoenix/util/json/BsonJsonProvider.java:
##########
@@ -0,0 +1,250 @@
+/*
+ * 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.phoenix.util.json;
+
+import com.jayway.jsonpath.InvalidJsonException;
+import com.jayway.jsonpath.spi.json.AbstractJsonProvider;
+import org.bson.BsonArray;
+import org.bson.BsonBinary;
+import org.bson.BsonBoolean;
+import org.bson.BsonDocument;
+import org.bson.BsonDouble;
+import org.bson.BsonInt32;
+import org.bson.BsonInt64;
+import org.bson.BsonObjectId;
+import org.bson.BsonString;
+import org.bson.BsonType;
+import org.bson.BsonValue;
+import org.bson.Document;
+import org.bson.json.JsonReader;
+import org.bson.types.ObjectId;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+public class BsonJsonProvider extends AbstractJsonProvider {
+
+    @Override
+    public Object parse(final String json) throws InvalidJsonException {
+        JsonReader jsonReader = new JsonReader(json);
+        BsonType bsonType = jsonReader.readBsonType();
+        switch (bsonType) {
+        case ARRAY:
+            return BsonArray.parse(json);
+        case DOCUMENT:
+            return BsonDocument.parse(json);
+        case STRING:
+            return new BsonString(jsonReader.readString());
+        case INT32:
+            return new BsonInt32(jsonReader.readInt32());
+        default:
+            throw new InvalidJsonException(String.format("Unsupported bson 
type %s", bsonType));
+        }
+    }
+
+    @Override
+    public Object parse(InputStream jsonStream, String charset) throws 
InvalidJsonException {
+        return null;
+    }
+
+    @Override
+    public String toJson(Object obj) {
+        return null;
+    }
+
+    @Override
+    public Object createArray() {
+        return new BsonArray();
+    }
+
+    @Override
+    public boolean isArray(final Object obj) {
+
+        return (obj instanceof BsonArray || obj instanceof List);
+    }
+
+    @Override
+    public Object getArrayIndex(final Object obj, final int idx) {
+
+        return toBsonArray(obj).get(idx);
+    }
+
+    @Override
+    public void setArrayIndex(final Object array, final int index, final 
Object newValue) {
+        if (!isArray(array)) {
+            throw new UnsupportedOperationException();
+        } else {
+            BsonArray arr = toBsonArray(array);
+            if (index == arr.size()) {
+                arr.add(toBsonValue(newValue));
+            } else {
+                arr.set(index, toBsonValue(newValue));
+            }
+        }
+    }
+
+    @Override
+    public Object createMap() {
+        return new BsonDocument();
+    }
+
+    @Override
+    public boolean isMap(final Object obj) {
+        return (obj instanceof BsonDocument);
+    }
+
+    @Override
+    public Object getMapValue(final Object obj, final String key) {
+        BsonDocument bsonDocument = toBsonDocument(obj);
+        Object o = bsonDocument.get(key);
+        if (!bsonDocument.containsKey(key)) {
+            return UNDEFINED;
+        } else {
+            return unwrap(o);
+        }
+    }
+
+    @Override
+    public Iterable<?> toIterable(final Object obj) {
+        BsonArray arr = toBsonArray(obj);
+        List<Object> values = new ArrayList<Object>(arr.size());
+        for (Object o : arr) {
+            values.add(toJavaType(toBsonValue(o)));
+        }
+        return values;
+    }
+
+    @Override
+    public void setProperty(final Object obj, final Object key, final Object 
value) {
+        if (isMap(obj)) {
+            toBsonDocument(obj).put(key.toString(), toBsonValue(value));
+        } else {
+            BsonArray array = toBsonArray(obj);
+            int index;
+            if (key != null) {
+                index = key instanceof Integer ? (Integer) key : 
Integer.parseInt(key.toString());
+            } else {
+                index = array.size();
+            }
+
+            if (index == array.size()) {
+                array.add(toBsonValue(value));
+            } else {
+                array.set(index, toBsonValue(value));
+            }
+        }
+    }
+
+    private static BsonArray toBsonArray(final Object o) {
+        return (BsonArray) o;
+    }
+
+    private static BsonDocument toBsonDocument(final Object o) {
+        return (BsonDocument) o;
+    }
+
+    /**
+     * 
https://github.com/spring-projects/spring-data-mongodb/blob/main/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/BsonUtils.java#L66

Review Comment:
   This is the reference code for this function implementation. We can actually 
remove this.





> Support native JSON data type
> -----------------------------
>
>                 Key: PHOENIX-628
>                 URL: https://issues.apache.org/jira/browse/PHOENIX-628
>             Project: Phoenix
>          Issue Type: Task
>    Affects Versions: 5.1.4
>            Reporter: James R. Taylor
>            Assignee: Ranganath Govardhanagiri
>              Labels: JSON, Java, SQL
>             Fix For: 4.4.1, 5.2.0
>
>         Attachments: JSON Support for Phoenix.docx, Screen Shot 2022-02-02 at 
> 12.23.24 PM.png, image-2023-12-07-11-26-56-198.png
>
>
> MongoDB and Postgres do some interesting things with JSON. We should look at 
> adding similar support. For a detailed description, see JSONB support in 
> Postgres: 
> [http://www.craigkerstiens.com/2014/03/24/Postgres-9.4-Looking-up]
> [http://www.depesz.com/2014/03/25/waiting-for-9-4-introduce-jsonb-a-structured-format-for-storing-json/]
> [http://michael.otacoo.com/postgresql-2/manipulating-jsonb-data-with-key-unique/]



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to