All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minidev.json.JsonMapper Maven / Gradle / Ivy

There is a newer version: 4.1.5
Show newest version
/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache, 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.gnu.org/licenses/lgpl-3.0.html
 *
 * 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 net.minidev.json;

import com.jn.easyjson.core.JSONBuilderProvider;
import com.jn.easyjson.core.JsonTreeNode;
import com.jn.easyjson.core.node.*;
import com.jn.langx.util.collection.Collects;
import com.jn.langx.util.function.Predicate;
import com.jn.langx.util.reflect.Reflects;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JsonMapper {
    public static Object fromJsonTreeNode(JsonTreeNode node) {
        return JsonTreeNodes.toXxxJson(node, new ToXxxJsonMapper() {
            @Override
            public Object mappingNull(JsonNullNode node) {
                return null;
            }

            @Override
            public Object mappingPrimitive(JsonPrimitiveNode node) {
                if (node.isBooleanNode()) {
                    return node.getAsBoolean();
                }
                if (node.isStringNode()) {
                    return node.getAsString();
                }
                if (node.isNumberNode()) {
                    return node.getAsNumber();
                }
                return node.getValue();
            }

            @Override
            public JSONArray mappingArray(JsonArrayNode arrayNode) {
                JSONArray array = new JSONArray();
                for (int i = 0; i < arrayNode.size(); i++) {
                    array.appendElement(JsonTreeNodes.toXxxJson(arrayNode.get(i), this));
                }
                return array;
            }

            @Override
            public JSONObject mappingObject(JsonObjectNode node) {
                JsonObjectNode objectNode = node.getAsJsonObjectNode();
                Iterator> iter = objectNode.propertySet().iterator();
                JSONObject map = new JSONObject();
                while (iter.hasNext()) {
                    Map.Entry entry = iter.next();
                    map.appendField(entry.getKey(), JsonTreeNodes.toXxxJson(entry.getValue(), this));
                }
                return map;
            }
        });
    }


    private static final List supportedTypes = Collects.newArrayList(
            JSONArray.class,
            JSONObject.class,
            JSONAware.class,
            JSONStreamAware.class
    );

    public static JsonTreeNode toJsonTreeNode(Object object) {
        return JsonTreeNodes.toJsonTreeNode(object, new ToJsonTreeNodeMapper() {
            @Override
            public boolean isAcceptable(final Object object) {
                return Collects.anyMatch(supportedTypes, new Predicate() {
                    @Override
                    public boolean test(Class value) {
                        return Reflects.isSubClassOrEquals(value, object.getClass());
                    }
                });
            }

            @Override
            public JsonTreeNode mapping(Object object) {
                if (object instanceof JSONArray) {
                    JsonArrayNode array = new JsonArrayNode();
                    JSONArray jsonArray = (JSONArray) object;
                    for (Object item : jsonArray) {
                        array.add(JsonTreeNodes.toJsonTreeNode(item, this));
                    }
                    return array;
                }

                if (object instanceof JSONObject) {
                    JsonObjectNode objectNode = new JsonObjectNode();
                    JSONObject obj = (JSONObject) object;
                    for (String key : obj.keySet()) {
                        objectNode.addProperty(key, JsonTreeNodes.toJsonTreeNode(obj.get(key), this));
                    }
                    return objectNode;
                }

                String jsonString = null;
                if (object instanceof JSONAware) {
                    jsonString = ((JSONAware) object).toJSONString();
                }
                if (object instanceof JSONStreamAware) {
                    StringBuilder stringBuilder = new StringBuilder();
                    try {
                        ((JSONStreamAware) object).writeJSONString(stringBuilder);
                    } catch (IOException e) {
                        // TODO log
                    }
                    jsonString = stringBuilder.toString();
                }
                if (jsonString != null) {
                    return JSONBuilderProvider.simplest().fromJson(jsonString);
                }
                return null;
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy