smiklosovic commented on code in PR #2297:
URL: https://github.com/apache/cassandra/pull/2297#discussion_r1180756022


##########
src/java/org/apache/cassandra/utils/JsonUtils.java:
##########
@@ -0,0 +1,204 @@
+/*
+ * 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.cassandra.utils;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.util.BufferRecyclers;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import org.apache.cassandra.io.util.File;
+import org.apache.cassandra.io.util.FileInputStreamPlus;
+import org.apache.cassandra.io.util.FileOutputStreamPlus;
+import org.apache.cassandra.serializers.MarshalException;
+
+import static org.apache.cassandra.io.util.File.WriteMode.OVERWRITE;
+
+public final class JsonUtils
+{
+    public static final ObjectMapper JSON_OBJECT_MAPPER = new ObjectMapper(new 
JsonFactory());
+
+    public static final ObjectWriter JSON_OBJECT_WRITER;
+
+    static
+    {
+        JSON_OBJECT_MAPPER.registerModule(new JavaTimeModule());
+        
JSON_OBJECT_MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+        JSON_OBJECT_WRITER = 
JSON_OBJECT_MAPPER.writerWithDefaultPrettyPrinter();
+    }
+
+    private JsonUtils()
+    {
+    }
+
+    /**
+     * Quotes string contents using standard JSON quoting.
+     */
+    public static String quoteAsJsonString(String s)
+    {
+        // In future should update to directly use 
`JsonStringEncoder.getInstance()` but for now:
+        return new 
String(BufferRecyclers.getJsonStringEncoder().quoteAsString(s));
+    }
+
+    public static Object decodeJson(byte[] json)
+    {
+        try
+        {
+            return JSON_OBJECT_MAPPER.readValue(json, Object.class);
+        }
+        catch (IOException exc)
+        {
+            throw new MarshalException("Error decoding JSON bytes: " + 
exc.getMessage());
+        }
+    }
+
+    public static Object decodeJson(String json)
+    {
+        try
+        {
+            return JSON_OBJECT_MAPPER.readValue(json, Object.class);
+        }
+        catch (IOException exc)
+        {
+            throw new MarshalException("Error decoding JSON string: " + 
exc.getMessage());
+        }
+    }
+
+    public static byte[] writeAsJsonBytes(Object value) throws MarshalException
+    {
+        try
+        {
+            return JSON_OBJECT_MAPPER.writeValueAsBytes(value);
+        }
+        catch (IOException exc)
+        {
+            throw new MarshalException("Error writing as JSON: " + 
exc.getMessage());
+        }
+    }
+
+    public static String writeAsJsonString(Object value) throws 
MarshalException
+    {
+        try
+        {
+            return JSON_OBJECT_MAPPER.writeValueAsString(value);
+        }
+        catch (IOException exc)
+        {
+            throw new MarshalException("Error writing as JSON: " + 
exc.getMessage());
+        }
+    }
+
+    public static String writeAsPrettyJsonString(Object value) throws 
MarshalException
+    {
+        try
+        {
+            return JSON_OBJECT_WRITER.writeValueAsString(value);
+        }
+        catch (IOException exc)
+        {
+            throw new MarshalException("Error writing as JSON: " + 
exc.getMessage());
+        }
+    }
+
+    public static Map<String, String> fromJsonMap(String json)
+    {
+        try
+        {
+            return JSON_OBJECT_MAPPER.readValue(json, Map.class);
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static List<String> fromJsonList(String json)
+    {
+        try
+        {
+            return JSON_OBJECT_MAPPER.readValue(json, List.class);
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * @deprecated Use org.apache.cassandra.cql3.Json#writeAsJsonString() 
instead
+     */
+    @Deprecated
+    public static String json(Object object)

Review Comment:
   this might just go away



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to