net.minidev.json.mapper.CollectionMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-smart Show documentation
Show all versions of json-smart Show documentation
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
package net.minidev.json.mapper;
/*
* Copyright 2011 JSON-SMART authors
*
* 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.
*/
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import net.minidev.asm.BeansAccess;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONUtil;
public class CollectionMapper {
public static class MapType extends AMapper {
final ParameterizedType type;
final Class> rawClass;
final Class> instance;
final BeansAccess> ba;
final Type keyType;
final Type valueType;
final Class> keyClass;
final Class> valueClass;
AMapper> subMapper;
public MapType(ParameterizedType type) {
this.type = type;
this.rawClass = (Class>) type.getRawType();
if (rawClass.isInterface())
instance = JSONObject.class;
else
instance = rawClass;
ba = BeansAccess.get(instance, JSONUtil.JSON_SMART_FIELD_FILTER);
keyType = type.getActualTypeArguments()[0];
valueType = type.getActualTypeArguments()[1];
if (keyType instanceof Class)
keyClass = (Class>) keyType;
else
keyClass = (Class>) ((ParameterizedType) keyType).getRawType();
if (valueType instanceof Class)
valueClass = (Class>) valueType;
else
valueClass = (Class>) ((ParameterizedType) valueType).getRawType();
}
@Override
public Object createObject() {
return ba.newInstance();
}
@Override
public AMapper> startArray(String key) {
if (subMapper == null)
subMapper = Mapper.getMapper(valueType);
return subMapper;
}
@Override
public AMapper> startObject(String key) {
if (subMapper == null)
subMapper = Mapper.getMapper(valueType);
return subMapper;
}
@SuppressWarnings("unchecked")
@Override
public void setValue(Object current, String key, Object value) {
((Map