org.json.simple.parser.ContainerFactory Java Examples

The following examples show how to use org.json.simple.parser.ContainerFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingleComponentDTO.java    From singleton with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Get the ordered SingleComponentDTO from one JSON string
 *
 * @param jsonStr One JSON string can convert to a SingleComponentDTO object
 * @return SingleComponentDTO
 */
@SuppressWarnings("unchecked")
public static SingleComponentDTO getSingleComponentDTOWithLinkedMessages(String jsonStr)
        throws ParseException {
    JSONParser parser = new JSONParser();
    ContainerFactory containerFactory = getContainerFactory();
    Map<String, Object> messages = new LinkedHashMap<String, Object>();
    Map<String, Object> bundle = null;
    bundle = (Map<String, Object>) parser.parse(jsonStr, containerFactory);
    if (bundle == null) {
        return null;
    }
    SingleComponentDTO baseComponentMessagesDTO = new SingleComponentDTO();
    baseComponentMessagesDTO.setId(Long.parseLong(bundle.get(ConstantsKeys.ID) == null ? "0" : bundle.get(ConstantsKeys.ID).toString()));
    baseComponentMessagesDTO.setComponent((String) bundle.get(ConstantsKeys.COMPONENT));
    baseComponentMessagesDTO.setLocale((String) bundle.get(ConstantsKeys.lOCALE));
    messages = (Map<String, Object>) bundle.get(ConstantsKeys.MESSAGES);
    baseComponentMessagesDTO.setMessages(messages);
    return baseComponentMessagesDTO;
}
 
Example #2
Source File: JsonSimpleConfigParser.java    From java-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T fromJson(String json, Class<T> clazz) throws JsonParseException {
    if (Map.class.isAssignableFrom(clazz)) {
        try {
            return (T)new JSONParser().parse(json, new ContainerFactory() {
                @Override
                public Map createObjectContainer() {
                    return new HashMap();
                }

                @Override
                public List creatArrayContainer() {
                    return new ArrayList();
                }
            });
        } catch (ParseException e) {
            throw new JsonParseException("Unable to parse JSON string: " + e.toString());
        }
    }

    // org.json.simple does not support parsing to user objects
    throw new JsonParseException("Parsing fails with a unsupported type");
}
 
Example #3
Source File: DcEngineDao.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * クライアントライブラリで、JavaのJSONObjectを直接扱えないためラップして返す.
 * @param jsonStr JSON文字列
 * @return JSONObjectのラッパー
 * @throws ParseException ParseException
 */
public final DcJSONObject newDcJSONObject(final String jsonStr) throws ParseException {
    DcJSONObject json = (DcJSONObject) (new JSONParser().parse(jsonStr, new ContainerFactory() {

        @SuppressWarnings("rawtypes")
        @Override
        public Map createObjectContainer() {
            return new DcJSONObject();
        }

        @SuppressWarnings("rawtypes")
        @Override
        public List creatArrayContainer() {
            return null;
        }
    }));
    return json;
}
 
Example #4
Source File: InvocationRequestProcessor.java    From container with Apache License 2.0 6 votes vote down vote up
/**
 * Parses and maps a json String to a {@literal LinkedHashMap<String, LinkedHashMap<String, String>>}.
 *
 * @return LinkedHashMap
 */
@SuppressWarnings("unchecked")
private LinkedHashMap<String, LinkedHashMap<String, String>> requestToMap(final String body) throws ParseException {

    final ContainerFactory orderedKeyFactory = new ContainerFactory() {

        @Override
        public Map<String, LinkedHashMap<String, String>> createObjectContainer() {
            return new LinkedHashMap<>();
        }

        @Override
        public List<?> creatArrayContainer() {
            // TODO Auto-generated method stub
            return null;
        }
    };

    final JSONParser parser = new JSONParser();

    final Object obj = parser.parse(body, orderedKeyFactory);

    return (LinkedHashMap<String, LinkedHashMap<String, String>>) obj;
}
 
Example #5
Source File: InvocationRequestProcessor.java    From container with Apache License 2.0 6 votes vote down vote up
/**
 * Parses and maps a json String to a {@literal LinkedHashMap<String, Object>}.
 *
 * @return LinkedHashMap
 */
private LinkedHashMap<String, Object> jsonStringToMap(final String jsonString) throws ParseException {

    final ContainerFactory orderedKeyFactory = new ContainerFactory() {
        @Override
        public LinkedHashMap<String, Object> createObjectContainer() {
            return new LinkedHashMap<>();
        }

        @Override
        public List<?> creatArrayContainer() {
            // TODO Auto-generated method stub
            return null;
        }
    };

    final JSONParser parser = new JSONParser();

    final Object obj = parser.parse(jsonString, orderedKeyFactory);

    return (LinkedHashMap<String, Object>) obj;
}
 
Example #6
Source File: InvocationRequestProcessor.java    From container with Apache License 2.0 6 votes vote down vote up
/**
 * Parses and maps a json String to a {@literal LinkedHashMap<String, LinkedHashMap<String, Object>>}.
 *
 * @return LinkedHashMap
 */
private LinkedHashMap<String, LinkedHashMap<String, Object>> requestToMap(final String body) throws ParseException {

    final ContainerFactory orderedKeyFactory = new ContainerFactory() {
        @Override
        public Map<String, LinkedHashMap<String, Object>> createObjectContainer() {
            return new LinkedHashMap<>();
        }

        @Override
        public List<?> creatArrayContainer() {
            // TODO Auto-generated method stub
            return null;
        }
    };

    final JSONParser parser = new JSONParser();

    final Object obj = parser.parse(body, orderedKeyFactory);

    return (LinkedHashMap<String, LinkedHashMap<String, Object>>) obj;
}
 
Example #7
Source File: jcoSon.java    From jcoSon with Apache License 2.0 6 votes vote down vote up
private void createJsonParser()
{
    this.parser = new JSONParser();
    containerFactory = new ContainerFactory()
    {
        @Override
        public List creatArrayContainer()
        {
          return new LinkedList();
        }
        @Override
        public Map createObjectContainer()
        {
          return new LinkedHashMap();
        }
    };
}
 
Example #8
Source File: MapUtil.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Get the factory which is used to create own object.
 * 
 * @return
 */
public static ContainerFactory getContainerFactory() {
	ContainerFactory containerFactory = new ContainerFactory() {
		public List<Object> creatArrayContainer() {
			return new LinkedList<Object>();
		}

		public Map<String, Object> createObjectContainer() {
			return new LinkedHashMap<String, Object>();
		}
	};
	return containerFactory;
}
 
Example #9
Source File: JSONUtil.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Convert Json string to Map<String, Object>
 * @param json
 * @return Map<String, Object> obj
 */
@SuppressWarnings("unchecked")
public static Map<String, Object> getMapFromJson(String json) {
    JSONParser parser = new JSONParser();
    ContainerFactory containerFactory = getContainerFactory();
    Map<String, Object> result = null;
    try {
        result = (Map<String, Object>) parser.parse(json, containerFactory);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example #10
Source File: JSONUtil.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
private static ContainerFactory getContainerFactory() {
    ContainerFactory containerFactory = new ContainerFactory() {
        public List<Object> creatArrayContainer() {
            return new LinkedList<Object>();
        }

        public Map<String, Object> createObjectContainer() {
            return new LinkedHashMap<String, Object>();
        }
    };
    return containerFactory;
}
 
Example #11
Source File: JSONUtils.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Convert Json string to Map<String, Object>
 * 
 * @param json
 * @return Map<String, Object> obj
 */
@SuppressWarnings("unchecked")
public static Map<String, Object> getMapFromJson(String json) {
	JSONParser parser = new JSONParser();
	ContainerFactory containerFactory = getContainerFactory();
	Map<String, Object> result = null;
	if (!StringUtils.isEmpty(json)) {
		try {
			result = (Map<String, Object>) parser.parse(json, containerFactory);
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}
	}
	return result;
}
 
Example #12
Source File: JSONUtils.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
private static ContainerFactory getContainerFactory() {
	ContainerFactory containerFactory = new ContainerFactory() {
		public List<Object> creatArrayContainer() {
			return new LinkedList<Object>();
		}

		public Map<String, Object> createObjectContainer() {
			return new LinkedHashMap<String, Object>();
		}
	};
	return containerFactory;
}
 
Example #13
Source File: SingleComponentDTO.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
private static ContainerFactory getContainerFactory() {
    ContainerFactory containerFactory = new ContainerFactory() {
        public List<Object> creatArrayContainer() {
            return new LinkedList<Object>();
        }

        public Map<String, Object> createObjectContainer() {
            return new LinkedHashMap<String, Object>();
        }
    };
    return containerFactory;
}
 
Example #14
Source File: SourceDaoImpl.java    From singleton with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public ComponentMessagesDTO mergeCacheWithBundle(
		ComponentSourceDTO cachedComponentSourceDTO, String componentJSON) {
	ComponentMessagesDTO componentMessagesDTO = new ComponentMessagesDTO();
	BeanUtils.copyProperties(cachedComponentSourceDTO, componentMessagesDTO);
	if (!StringUtils.isEmpty(componentJSON)) {
		JSONParser parser = new JSONParser();
		ContainerFactory containerFactory = MapUtil.getContainerFactory();
		Map<String, Object> messages = new LinkedHashMap<String, Object>();
		Map<String, Object> bundle = null;
		try {
			bundle = (Map<String, Object>) parser.parse(componentJSON,
					containerFactory);
		} catch (ParseException e) {
			LOGGER.error(e.getMessage(), e);
			
		}
		if ( (bundle != null) && !StringUtils.isEmpty(bundle)) {
			messages = (Map<String, Object>) bundle
					.get(ConstantsKeys.MESSAGES);
			Iterator<Map.Entry<String, Object>> it = cachedComponentSourceDTO
					.getMessages().entrySet().iterator();
			boolean isChanged = false ;
			while (it.hasNext()) {
				Map.Entry<String, Object> entry = it.next();
				String key = entry.getKey();
				String value = entry.getValue() == null ? "" : (String)entry.getValue();
				String v = messages.get(key) == null ? "" : (String)messages.get(key);
				if(!value.equals(v) && !isChanged) {
					isChanged = true;
				}
				MapUtil.updateKeyValue(messages, key, value);
			}
			if(isChanged) {
				componentMessagesDTO.setId(System.currentTimeMillis());
			} else {
				componentMessagesDTO.setId(Long.parseLong(bundle.get(ConstantsKeys.ID) == null ? "0" : bundle.get(ConstantsKeys.ID).toString()));
			}
			componentMessagesDTO.setMessages(messages);
		}
	}
	return componentMessagesDTO;
}