List of usage examples for com.fasterxml.jackson.databind JsonMappingException JsonMappingException
public JsonMappingException(String paramString, Throwable paramThrowable)
From source file:com.bitsofproof.dropwizard.supernode.jackson.AddressSerializer.java
@Override public void serialize(Address value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException { try {//from w ww . j a v a2 s . c om jgen.writeString(Address.toSatoshiStyle(value)); } catch (ValidationException e) { throw new JsonMappingException("Error serializing address", e); } }
From source file:org.springframework.batch.admin.web.JsonWrapper.java
@SuppressWarnings("unchecked") public JsonWrapper(String content) throws Exception { this.content = content; try {/*from ww w .j a va 2s . c o m*/ target = new MappingJsonFactory().createJsonParser(content.replace("\\", "/")).readValueAs(Map.class); } catch (JsonParseException e) { throw new JsonMappingException("Cannot create wrapper for:\n" + content, e); } context = new StandardEvaluationContext(); context.addPropertyAccessor(new MapAccessor()); parser = new SpelExpressionParser(); }
From source file:reactor.js.core.json.JSObjectDeserializer.java
@SuppressWarnings("unchecked") @Override/* w w w. j a va 2s . c o m*/ public JSObject deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Object obj = jp.readValueAs(Object.class); Object parent = ctxt.getAttribute("parent"); ctxt.setAttribute("parent", obj); if (Map.class.isInstance(obj)) { return new JavaScriptObject((Map) obj, null, null, parent); } else if (List.class.isInstance(obj)) { return new JavaScriptArray((List) obj); } else { throw new JsonMappingException("Cannot convert value to a valid JSON object", jp.getCurrentLocation()); } }
From source file:com.netflix.discovery.converters.jackson.serializer.PortWrapperXmlDeserializer.java
@Override public InstanceInfo.PortWrapper deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { boolean enabled = false; int port = 0; while (jp.nextToken() == JsonToken.FIELD_NAME) { String fieldName = jp.getCurrentName(); jp.nextToken(); // to point to value if ("enabled".equals(fieldName)) { enabled = Boolean.valueOf(jp.getValueAsString()); } else if (fieldName == null || "".equals(fieldName)) { String value = jp.getValueAsString(); port = value == null ? 0 : Integer.parseInt(value); } else {/*ww w.j a v a 2 s . com*/ throw new JsonMappingException("Unexpected field " + fieldName, jp.getCurrentLocation()); } } return new InstanceInfo.PortWrapper(enabled, port); }
From source file:com.netflix.discovery.converters.jackson.serializer.ApplicationXmlDeserializer.java
@Override public Application deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String name = null;//from w ww .j a v a 2 s .c o m List<InstanceInfo> instances = new ArrayList<>(); while (jp.nextToken() == JsonToken.FIELD_NAME) { String fieldName = jp.getCurrentName(); jp.nextToken(); // to point to value if ("name".equals(fieldName)) { name = jp.getValueAsString(); } else if ("instance".equals(fieldName)) { instances.add(jp.readValueAs(InstanceInfo.class)); } else { throw new JsonMappingException("Unexpected field " + fieldName, jp.getCurrentLocation()); } } return new Application(name, instances); }
From source file:com.arpnetworking.metrics.impl.TsdLogSinkTest.java
@Test public void testObjectMapperIOException() throws IOException { final org.slf4j.Logger logger = createSlf4jLoggerMock(); final ObjectMapper objectMapper = Mockito.spy(new ObjectMapper()); final Sink sink = new TsdLogSink( new TsdLogSink.Builder().setDirectory(createDirectory("./target/TsdLogSinkTest")) .setName("testObjectMapperIOException-Query"), objectMapper, logger);// w w w .j a va2s . co m Mockito.doThrow(new JsonMappingException(Mockito.mock(JsonParser.class), "JsonMappingException")) .when(objectMapper).writeValueAsString(Mockito.any()); recordEmpty(sink); Mockito.verify(logger).warn(Mockito.argThat(Matchers.any(String.class)), Mockito.argThat(Matchers.any(Throwable.class))); }
From source file:com.arpnetworking.metrics.impl.StenoLogSinkTest.java
@Test public void testObjectMapperIOException() throws IOException { final org.slf4j.Logger logger = createSlf4jLoggerMock(); final ObjectMapper objectMapper = Mockito.spy(new ObjectMapper()); final Sink sink = new StenoLogSink( new StenoLogSink.Builder().setDirectory(createDirectory("./target/StenoLogSinkTest")) .setName("testObjectMapperIOException-Query"), objectMapper, logger);/*from w w w . j av a2 s. com*/ Mockito.doThrow(new JsonMappingException(Mockito.mock(JsonParser.class), "JsonMappingException")) .when(objectMapper).writeValueAsString(Mockito.any()); recordEmpty(sink); Mockito.verify(logger).warn(Mockito.argThat(Matchers.any(String.class)), Mockito.argThat(Matchers.any(Throwable.class))); Mockito.verifyNoMoreInteractions(logger); }
From source file:com.basistech.rosette.dm.jackson.MorphoAnalysisListDeserializer.java
@Override public List<MorphoAnalysis> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { if (!cached) { throw new JsonMappingException(jp, "attempt to deserialize with un-contextualized MorphoAnalysisListDeserializer"); }//from w w w. java2s .c o m /* * This will be entered pointing to the array start. */ if (jp.getCurrentToken() != JsonToken.START_ARRAY) { throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "Expected array of items"); } JsonDeserializer<Object> currentDeserializer = castDeserializer( ctxt.getAttribute(MorphoAnalysisListDeserializer.class)); if (currentDeserializer == null) { currentDeserializer = maDeserializer; ctxt.setAttribute(MorphoAnalysisListDeserializer.class, maDeserializer); } List<MorphoAnalysis> result = Lists.newArrayList(); while (jp.nextToken() != JsonToken.END_ARRAY) { // if we just read it in as the wrong class, any leftovers will end up in extendedAttributes, and we can cope. MorphoAnalysis analysis = (MorphoAnalysis) currentDeserializer.deserialize(jp, ctxt); if (analysis.getExtendedProperties().size() != 0) { // so, we have leftovers. Note that this will not trim han and arabic down. Tough if (analysis.getExtendedProperties().containsKey("morphemes")) { KoreanMorphoAnalysis.Builder builder = new KoreanMorphoAnalysis.Builder(); copyBasic(analysis, builder); List<String> morphemes = cast(analysis.getExtendedProperties().get("morphemes")); List<String> morphemeTags = cast(analysis.getExtendedProperties().get("morphemeTags")); for (int x = 0; x < morphemes.size(); x++) { builder.addMorpheme(morphemes.get(x), morphemeTags.get(x)); } for (Map.Entry<String, Object> me : analysis.getExtendedProperties().entrySet()) { if (!"morphemes".equals(me.getKey()) && !"morphemeTags".equals(me.getKey())) { builder.extendedProperty(me.getKey(), me.getValue()); } } analysis = builder.build(); ctxt.setAttribute(MorphoAnalysisListDeserializer.class, korMaDeserializer); } else if (analysis.getExtendedProperties().containsKey("readings")) { // convert to Han. HanMorphoAnalysis.Builder builder = new HanMorphoAnalysis.Builder(); copyBasic(analysis, builder); for (String reading : cast(analysis.getExtendedProperties().get("readings"))) { builder.addReading(reading); } for (Map.Entry<String, Object> me : analysis.getExtendedProperties().entrySet()) { if (!"readings".equals(me.getKey())) { builder.extendedProperty(me.getKey(), me.getValue()); } } analysis = builder.build(); ctxt.setAttribute(MorphoAnalysisListDeserializer.class, hanMaDeserializer); } else if (anyArabicFields(analysis.getExtendedProperties().keySet())) { ArabicMorphoAnalysis.Builder builder = new ArabicMorphoAnalysis.Builder(); copyBasic(analysis, builder); Integer prefixLength = (Integer) analysis.getExtendedProperties().get("prefixLength"); Integer stemLength = (Integer) analysis.getExtendedProperties().get("stemLength"); if (prefixLength != null && stemLength != null) { builder.lengths(prefixLength, stemLength); } String root = (String) analysis.getExtendedProperties().get("root"); if (root != null) { builder.root(root); } Boolean definiteArticle = (Boolean) analysis.getExtendedProperties().get("definiteArticle"); if (definiteArticle != null) { builder.definiteArticle(definiteArticle); } Boolean strippablePrefix = (Boolean) analysis.getExtendedProperties().get("strippablePrefix"); if (strippablePrefix != null) { builder.strippablePrefix(strippablePrefix); } List<String> prefixes = cast(analysis.getExtendedProperties().get("prefixes")); if (prefixes != null) { List<String> prefixTags = cast(analysis.getExtendedProperties().get("prefixTags")); for (int x = 0; x < prefixes.size(); x++) { builder.addPrefix(prefixes.get(x), prefixTags.get(x)); } } List<String> stems = cast(analysis.getExtendedProperties().get("stems")); if (stems != null) { List<String> stemTags = cast(analysis.getExtendedProperties().get("stemTags")); for (int x = 0; x < stems.size(); x++) { builder.addStem(stems.get(x), stemTags.get(x)); } } List<String> suffixes = cast(analysis.getExtendedProperties().get("suffixes")); if (suffixes != null) { List<String> suffixTags = cast(analysis.getExtendedProperties().get("suffixTags")); for (int x = 0; x < suffixes.size(); x++) { builder.addSuffix(suffixes.get(x), suffixTags.get(x)); } } for (Map.Entry<String, Object> me : analysis.getExtendedProperties().entrySet()) { if (!ARABIC_FIELDS.contains(me.getKey())) { builder.extendedProperty(me.getKey(), me.getValue()); } } analysis = builder.build(); ctxt.setAttribute(MorphoAnalysisListDeserializer.class, arMaDeserializer); } } result.add(analysis); } return ImmutableList.copyOf(result); }
From source file:com.xiongyingqi.spring.mvc.method.annotation.RequestJsonParamMethodArgumentResolver.java
@Override protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest webRequest) throws Exception { String[] paramValues = webRequest.getParameterValues(name); Class<?> paramType = parameter.getParameterType(); if (paramValues == null) { return null; }//from ww w .j a v a 2 s .c om // ? // for (int i = 0; i < paramValues.length; i++) { // String paramValue = paramValues[i]; // String encodedParamValue = new String(paramValue.getBytes("ISO-8859-1"), encoding); // paramValues[i] = encodedParamValue; // } try { if (paramValues.length == 1) { String text = paramValues[0]; Type type = parameter.getGenericParameterType(); if (MapWapper.class.isAssignableFrom(paramType)) { MapWapper<?, ?> jsonMap = (MapWapper<?, ?>) paramType.newInstance(); MapType mapType = (MapType) getJavaType(HashMap.class); if (type instanceof ParameterizedType) { mapType = (MapType) mapType .narrowKey((Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]); mapType = (MapType) mapType.narrowContentsBy( (Class<?>) ((ParameterizedType) type).getActualTypeArguments()[1]); } jsonMap.setInnerMap(mapper.<Map>readValue(text, mapType)); return jsonMap; } JavaType javaType = getJavaType(paramType); if (Collection.class.isAssignableFrom(paramType)) { javaType = javaType .narrowContentsBy((Class<?>) ((ParameterizedType) type).getActualTypeArguments()[0]); } return mapper.readValue(paramValues[0], javaType); } } catch (Exception e) { throw new JsonMappingException("Could not read request json parameter", e); } throw new UnsupportedOperationException("too many request json parameter '" + name + "' for method parameter type [" + paramType + "], only support one json parameter"); }
From source file:com.github.shyiko.jackson.module.advice.AdvisedBeanSerializer.java
protected void serializeFieldsFiltered(Object bean, JsonGenerator jgen, SerializerProvider provider) throws IOException { /* note: almost verbatim copy of "serializeFields"; copied (instead of merged) * so that old method need not add check for existence of filter. *///w w w . j a va 2 s .c om final BeanPropertyWriter[] props; if (_filteredProps != null && provider.getActiveView() != null) { props = _filteredProps; } else { props = _props; } final PropertyFilter filter = findPropertyFilter(provider, _propertyFilterId, bean); // better also allow missing filter actually.. if (filter == null) { serializeFields(bean, jgen, provider); return; } int i = 0; try { for (final int len = props.length; i < len; ++i) { BeanPropertyWriter prop = props[i]; if (prop != null) { // can have nulls in filtered list if (!beanSerializerAdvice.intercept(bean, jgen, prop, provider)) { beanSerializerAdvice.before(bean, jgen, prop, provider); filter.serializeAsField(bean, jgen, provider, prop); beanSerializerAdvice.after(bean, jgen, prop, provider); } } } if (_anyGetterWriter != null) { _anyGetterWriter.getAndFilter(bean, jgen, provider, filter); } } catch (Exception e) { String name = (i == props.length) ? "[anySetter]" : props[i].getName(); wrapAndThrow(provider, e, bean, name); } catch (StackOverflowError e) { JsonMappingException mapE = new JsonMappingException("Infinite recursion (StackOverflowError)", e); String name = (i == props.length) ? "[anySetter]" : props[i].getName(); mapE.prependPath(new JsonMappingException.Reference(bean, name)); throw mapE; } }