com.datastax.stargate.sdk.utils.JsonUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stargate-sdk Show documentation
Show all versions of stargate-sdk Show documentation
Connect to Stargate Data Gateway
/*
* Copyright DataStax, Inc.
*
* Licensed 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 com.datastax.stargate.sdk.utils;
import java.util.Collection;
import java.util.Map;
/**
* Custom implementation of serialization : faster + no jackson dependency
*
* @author Cedrick Lunven (@clunven)
*/
public class JsonUtils {
private JsonUtils() {
}
/**
* Code is built based on https://github.com/fangyidong/json-simple/blob/master/src/main/java/org/json/simple/JSONValue.java
* by FangYidong [email protected] THANK YOU ! ff4j core needs to stay with no dependency.
*
*
* The following characters are reserved characters and can not be used in JSON and must be properly escaped to be used in strings.
* - Backspace to be replaced with \b
* - Form feed to be replaced with \f
* - Newline to be replaced with \n
* - Carriage return to be replaced with \r
* - Tab to be replaced with \t
* - Double quote to be replaced with \"
* - Backslash to be replaced with \\
*
* @param value
* string to be escaped
* @return
* escaped JSON
*/
public static final String escapeJson(String value) {
if (value == null ) return null;
StringBuilder output = new StringBuilder();
final int len = value.length();
for(int i=0;i='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
String ss=Integer.toHexString(ch);
output.append("\\u");
for(int k=0;k<4-ss.length();k++){
output.append('0');
}
output.append(ss.toUpperCase());
}
else{
output.append(ch);
}
}
}
return output.toString();
}
/**
* Target primitive displayed as JSON.
*
* @param value
* object value
* @return
* target json expression
*/
public static final String valueAsJson(Object value) {
if (value == null ) return "null";
if (value instanceof String) return "\"" + escapeJson(value.toString()) + "\"";
return value.toString();
}
/**
* Serialize a collection of object as Json. Element should eventually override toString()
to produce JSON.
*
* @param T
* @param pCollec input collection
* @return collection as String
*/
public static final String collectionAsJson(final Collection < T > pCollec) {
if (pCollec == null) return "null";
if (pCollec.isEmpty()) return "[]";
StringBuilder json = new StringBuilder("[");
boolean first = true;
for (T element : pCollec) {
json.append(first ? "" : ",");
json.append(valueAsJson(element));
first = false;
}
json.append("]");
return json.toString();
}
/**
* Serialize a map of objects as Json. Elements should override toString()
to produce JSON.
*
* @param K
* @param V
* @param pMap target properties
* @return target json expression
*/
public static final String mapAsJson(final Map pMap) {
if (pMap == null) return "null";
if (pMap.isEmpty()) return "{}";
StringBuilder json = new StringBuilder("{");
boolean first = true;
for (Map.Entry mapEntry : pMap.entrySet()) {
json.append(first ? "" : ",");
json.append(valueAsJson(mapEntry.getKey()) + ":");
json.append(valueAsJson(mapEntry.getValue()));
first = false;
}
json.append("}");
return json.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy