spinjar.com.minidev.json.writer.CollectionMapper Maven / Gradle / Ivy
package net.minidev.json.writer;
/*
* 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 JsonReaderI {
final ParameterizedType type;
final Class> rawClass;
final Class> instance;
final BeansAccess> ba;
final Type keyType;
final Type valueType;
final Class> keyClass;
final Class> valueClass;
JsonReaderI> subMapper;
public MapType(JsonReader base, ParameterizedType type) {
super(base);
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() {
try {
return instance.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
@Override
public JsonReaderI> startArray(String key) {
if (subMapper == null)
subMapper = base.getMapper(valueType);
return subMapper;
}
@Override
public JsonReaderI> startObject(String key) {
if (subMapper == null)
subMapper = base.getMapper(valueType);
return subMapper;
}
@SuppressWarnings("unchecked")
@Override
public void setValue(Object current, String key, Object value) {
((Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy