org.lappsgrid.json2json.jsonobject.JacksonJsonProxy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json2json Show documentation
Show all versions of json2json Show documentation
Json2Json is Json transformation Java library, which use template transformation design.
The newest version!
/**********************************************************************************************************************
Copyright [2014] [Chunqi SHI ([email protected])]
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 org.lappsgrid.json2json.jsonobject;
import java.io.IOException;
import java.util.*;
import org.lappsgrid.json2json.jsonobject.JsonProxy.JsonArray;
import org.lappsgrid.json2json.jsonobject.JsonProxy.JsonObject;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Jackson Json provides best implementation of Json functions
* Jackson Json.
*
*/
public class JacksonJsonProxy implements JsonProxy.NewProxy {
public static Object valueOf(Object obj) {
if (obj instanceof JacksonJsonObject) {
return ((JacksonJsonObject) obj).original();
} else if (obj instanceof JacksonJsonArray) {
return ((JacksonJsonArray) obj).original();
}
return obj;
}
public static Object value2object(Object obj ) {
if(obj instanceof List) {
return new JacksonJsonArray((List)obj);
} else if(obj instanceof Map) {
return new JacksonJsonObject((Map)obj);
}
return obj;
}
@Override
public JsonArray newArray() {
return new JacksonJsonArray();
}
@Override
public JsonObject newObject() {
return new JacksonJsonObject();
}
public static class JacksonJsonObject implements JsonObject {
Map map = null;
public JacksonJsonObject() {
map = new LinkedHashMap();
}
public JacksonJsonObject(Map map) {
this.map = map;
}
@Override
public JsonObject read(String s) {
ObjectMapper mapper = new ObjectMapper();
try {
map = (Map)mapper.readValue(s, LinkedHashMap.class);
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
@Override
public boolean has(String key) {
return map.containsKey(key);
}
@Override
public Object get(String key) {
return value2object(map.get(key));
}
@Override
public JsonObject put(String key, Object val) {
map.put(key, valueOf(val));
return this;
}
@Override
public JsonObject remove(String key) {
map.remove(key);
return this;
}
@Override
public int length() {
return map.size();
}
@Override
public Collection keys() {
return map.keySet();
}
@Override
public Object original() {
return map;
}
// @Override
// public JsonObject clone() {
// return new JacksonJsonObject(new LinkedHashMap(map));
// }
@Override
public String toString() {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(this.map);
}catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public static class JacksonJsonArray implements JsonArray{
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy