将json数据读取为带泛型的map类型的数据

@param content   json数据
@param mapType   要返回的map类型
@param keyType   map的key的泛型
@param valueType map的value的泛型
@param <T>       Map的实际类型
@param <K>       map中key的实际类型
@param <V>       map中value的实际类型
@return map 解析结果
540406
public <T extends Map<K, V>, K, V> T readAsMap(byte[] content, Class<? extends Map> mapType,
                                                   Class<K> keyType, Class<V> valueType) {
        return readAsMap(new String(content), mapType, keyType, valueType);
    }
164861
private String createValueFromMap(Map<String, String> valueMap) {

        if (valueMap == null) {
            return null;
        }
        StringBuffer result = new StringBuffer(valueMap.size() * 32);
        Iterator<Map.Entry<String, String>> i = valueMap.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<String, String> entry = i.next();
            String key = entry.getKey();
            String value = entry.getValue();
            key = replaceDelimiter(key, VALUE_LIST_DELIMITER, VALUE_LIST_DELIMITER_REPLACEMENT);
            key = replaceDelimiter(key, VALUE_MAP_DELIMITER, VALUE_MAP_DELIMITER_REPLACEMENT);
            value = replaceDelimiter(value, VALUE_LIST_DELIMITER, VALUE_LIST_DELIMITER_REPLACEMENT);
            value = replaceDelimiter(value, VALUE_MAP_DELIMITER, VALUE_MAP_DELIMITER_REPLACEMENT);
            result.append(key);
            result.append(VALUE_MAP_DELIMITER);
            result.append(value);
            if (i.hasNext()) {
                result.append(VALUE_LIST_DELIMITER);
            }
        }
        return result.toString();
    }
6993
@Override
  public JsonElement serialize(CMAEntry src, Type type, JsonSerializationContext context) {
    JsonObject fields = new JsonObject();
    for (Map.Entry<String, LinkedHashMap<String, Object>> field : src.getFields().entrySet()) {
      LinkedHashMap<String, Object> value = field.getValue();
      if (value == null) {
        continue;
      }
      String fieldId = field.getKey();
      JsonObject jsonField = serializeField(context, field.getValue());
      if (jsonField != null) {
        fields.add(fieldId, jsonField);
      }
    }

    JsonObject result = new JsonObject();
    result.add("fields", fields);

    final CMASystem sys = src.getSystem();
    if (sys != null) {
      result.add("sys", context.serialize(sys));
    }
    return result;
  }
266805
def postmap(self, path, valuemap=None, query=None):
        
        code, data, ctype = self.post(path, valuemap, query)
        if ctype != 'application/json':
            self.log.error("Expecting JSON from POST of '%s', got '%s'", self.lastpath, ctype)
            raise HttpError(code=400, content_type='text/plain', content='Remote returned invalid content type: '+ctype)
        try:
            result = json.loads(data)
        except Exception as e:                                                                  # pragma: no cover
            self.log.error("Could not load JSON content from POST %r -- %s", self.lastpath, e)
            raise HttpError(code=400, content_type='text/plain', content='Could not load JSON content')
        return result
49816
private TypeVarToTypeMap mapTypes( TypeVarToTypeMap actualParamByVarName, IType... types )
  {
    for( int i = 0; i < types.length; i++ )
    {
      IType type = types[i];
      if( type instanceof ITypeVariableType )
      {
        actualParamByVarName.put( (ITypeVariableType)types[i], types[i] );
      }
      if( type instanceof ITypeVariableArrayType )
      {
        mapTypes( actualParamByVarName, type.getComponentType() );
      }
      if( type.isParameterizedType() )
      {
        IType[] parameters = type.getTypeParameters();
        mapTypes( actualParamByVarName, parameters );
      }
      if( type instanceof IFunctionType )
      {
        IFunctionType funType = (IFunctionType)type;
        mapTypes( actualParamByVarName, funType.getReturnType() );
        IType[] paramTypes = funType.getParameterTypes();
        for( IType paramType : paramTypes )
        {
          mapTypes( actualParamByVarName, paramType );
        }
      }
    }
    return actualParamByVarName;
  }
857217
public static MessageSelectorBuilder fromKeyValueMap(Map<String, Object> valueMap) {
        StringBuffer buf = new StringBuffer();

        Iterator<Entry<String, Object>> iter = valueMap.entrySet().iterator();

        if (iter.hasNext()) {
            Entry<String, Object> entry = iter.next();
            String key = entry.getKey();
            String value = entry.getValue().toString();

            buf.append(key + " = '" + value + "'");
        }

        while (iter.hasNext()) {
        	Entry<String, Object> entry = iter.next();
        	String key = entry.getKey();
            String value = entry.getValue().toString();

            buf.append(" AND " + key + " = '" + value + "'");
        }

        return new MessageSelectorBuilder(buf.toString());
    }
833880
static javax.json.JsonValue map(final JsonValue jsonValue) {
        if (jsonValue instanceof JsonArray)
            return map((JsonArray) jsonValue);
        return map((JsonObject) jsonValue);
    }
1234220
public static <K, V> Bindable<Map<K, V>> mapOf(Class<K> keyType, Class<V> valueType) {
		return of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType));
	}
1765
private Map<String, CmsTemplateContext> initMap() throws Exception {

        Map<String, CmsTemplateContext> result = new LinkedHashMap<String, CmsTemplateContext>();
        String path = getConfigurationPropertyPath();
        CmsResource resource = m_cms.readResource(path);
        CmsFile file = m_cms.readFile(resource);
        String fileContent = new String(file.getContents(), "UTF-8");
        CmsMacroResolver resolver = new CmsMacroResolver();
        resolver.setCmsObject(m_cms);
        for (Map.Entry<String, String> param : m_params.entrySet()) {
            resolver.addMacro(param.getKey(), param.getValue());
        }
        fileContent = resolver.resolveMacros(fileContent);
        JSONTokener tok = new JSONTokener(fileContent);
        tok.setOrdered(true);
        JSONObject root = new JSONObject(tok, true);
        for (String templateContextName : root.keySet()) {
            JSONObject templateContextJson = (JSONObject)(root.opt(templateContextName));
            CmsJsonMessageContainer jsonMessage = new CmsJsonMessageContainer(templateContextJson.opt(A_NICE_NAME));
            String templatePath = (String)templateContextJson.opt(A_PATH);
            JSONObject variantsJson = (JSONObject)templateContextJson.opt(A_VARIANTS);
            List<CmsClientVariant> variants = new ArrayList<CmsClientVariant>();
            if (variantsJson != null) {
                for (String variantName : variantsJson.keySet()) {
                    JSONObject variantJson = (JSONObject)variantsJson.opt(variantName);
                    CmsJsonMessageContainer variantMessage = new CmsJsonMessageContainer(variantJson.opt(A_NICE_NAME));
                    int width = variantJson.optInt(A_WIDTH, 800);
                    int height = variantJson.optInt(A_HEIGHT, 600);
                    CmsClientVariant variant = new CmsClientVariant(
                        variantName,
                        variantMessage,
                        width,
                        height,
                        new HashMap<String, String>());
                    variants.add(variant);
                }
            }
            CmsTemplateContext templateContext = new CmsTemplateContext(
                templateContextName,
                templatePath,
                jsonMessage,
                this,
                variants,
                false);
            result.put(templateContextName, templateContext);

        }
        return result;
    }
1289716
public static <K, V> Map<K, V> copy(final Map<K, V> map) {
		if (map == null) {
			return null;
		}

		TypeEnum keyType = null;
		TypeEnum valueType = null;

		Map<K, V> copy = new HashMap<K, V>();
		for (Map.Entry<K, V> entry : map.entrySet()) {
			K key = entry.getKey();
			V value = entry.getValue();

			K keyCopy = null;
			if (key != null) {
				if (keyType == null) {
					keyType = TypeEnum.dataTypeOf(key.getClass());
				}
				keyCopy = copy(key, keyType);
			}

			V valueCopy = null;
			if (value != null) {
				if (valueType == null) {
					valueType = TypeEnum.dataTypeOf(value.getClass());
				}
				valueCopy = copy(value, valueType);
			}

			copy.put(keyCopy, valueCopy);
		}

		return copy;
	}
925655
public static <T> MemcachedClientBuilder<T> newJsonClient(final Class<T> valueType) {
    return newJsonClient(DefaultObjectMapperHolder.INSTANCE, valueType);
  }
494733
public static <M extends Multimap<?, ?>> MultimapJsonSerializer<M, ?, ?> newInstance( KeySerializer<?> keySerializer,
                                                                                          JsonSerializer<?> valueSerializer ) {
        return new MultimapJsonSerializer( keySerializer, valueSerializer );
    }
1039629
<T> T callForJsonType(final String host, final int port, final String path,
      final List<Pair<String, String>> paramList, final Class<T> valueType) throws IOException {
    final String responseString = callForJsonString(host, port, path, paramList);
    if (null == responseString || responseString.length() == 0) {
      return null;
    }
    return new ObjectMapper().readValue(responseString, valueType);
  }
1150805
public <C> C mapTo(Class<C> type) {
    DataFormatMapper mapper = dataFormat.getMapper();
    return mapper.mapInternalToJava(jsonNode, type);
  }
1277813
public static <K, V> ImmutableMap<K, V> loadMap(final File file,
      final Function<String, K> keyFunction, final Function<String, V> valueFunction)
      throws IOException {
    return loadMap(Files.asCharSource(file, Charsets.UTF_8), keyFunction, valueFunction);
  }
883363
public static <T> JacksonParser<T> json(Class<T> contentType) {
        return new JacksonParser<>(null, contentType);
    }
607670
public <T extends Mapping> T getMapping(MappingType type)
    {
        return (T) mappings.get(type);
    }