@SuppressWarnings("unchecked") public static JsonSerializer<Object> getStdKeySerializer(JavaType keyType) { if (keyType == null) { return DEFAULT_KEY_SERIALIZER; } Class<?> cls = keyType.getRawClass(); if (cls == String.class) { return DEFAULT_STRING_SERIALIZER; } if (cls == Object.class || cls.isPrimitive() || Number.class.isAssignableFrom(cls)) { return DEFAULT_KEY_SERIALIZER; } if (Date.class.isAssignableFrom(cls)) { return (JsonSerializer<Object>) DateKeySerializer.instance; } if (Calendar.class.isAssignableFrom(cls)) { return (JsonSerializer<Object>) CalendarKeySerializer.instance; } /* 14-Mar-2014, tatu: Should support @JsonValue, as per #47; but that * requires extensive introspection, and passing in more information * to this method. */ // If no match, just use default one: return DEFAULT_KEY_SERIALIZER; }
protected void assertType(Object ob, Class<?> expType) { if (ob == null) { fail("Expected an object of type " + expType.getName() + ", got null"); } Class<?> cls = ob.getClass(); if (!expType.isAssignableFrom(cls)) { fail("Expected type " + expType.getName() + ", got " + cls.getName()); } }
@SuppressWarnings("unchecked") public static <T> T convert(Context context, Class<T> type) { if (type.isAssignableFrom(Context.class)) { return (T) context; } if (type.isAssignableFrom(Map.class)) { return (T) context.keyValues(); } if (isUrlEncodedForm(context)) { return convertValue(context.keyValues(), type); } String json; try { json = context.request().getContent(); } catch (IOException e) { throw new IllegalArgumentException("Unable read request content", e); } return fromJson(json, type); }
/** * Helper method for constructing exception to indicate that input JSON Number was not suitable * for deserializing into given target type. */ public JsonMappingException weirdNumberException(Number value, Class<?> instClass, String msg) { return InvalidFormatException.from( _parser, "Can not construct instance of " + instClass.getName() + " from number value (" + _valueDesc() + "): " + msg, null, instClass); }
/** * Helper method for constructing exception to indicate that given JSON Object field name was not * in format to be able to deserialize specified key type. */ public JsonMappingException weirdKeyException(Class<?> keyClass, String keyValue, String msg) { return InvalidFormatException.from( _parser, "Can not construct Map key of type " + keyClass.getName() + " from String \"" + _desc(keyValue) + "\": " + msg, keyValue, keyClass); }
/** * Method that will construct an exception suitable for throwing when some String values are * acceptable, but the one encountered is not. * * @param value String value from input being deserialized * @param instClass Type that String should be deserialized into * @param msg Message that describes specific problem * @since 2.1 */ public JsonMappingException weirdStringException(String value, Class<?> instClass, String msg) { return InvalidFormatException.from( _parser, "Can not construct instance of " + instClass.getName() + " from String value '" + _valueDesc() + "': " + msg, value, instClass); }
public <T> T getWith(DrillConfig config, Class<T> c) { try { // logger.debug("Read tree {}", root); return config.getMapper().treeToValue(root, c); } catch (JsonProcessingException e) { throw new LogicalPlanParsingException( String.format( "Failure while trying to convert late bound json options to type of %s. Reference was originally located at line %d, column %d.", c.getCanonicalName(), location.getLineNr(), location.getColumnNr()), e); } }
protected Server startServer(int port, Class<? extends Application> appClass) { Server server = new Server(port); final ContextHandlerCollection contexts = new ContextHandlerCollection(); server.setHandler(contexts); ServletHolder jaxrs = new ServletHolder(ServletContainer.class); jaxrs.setInitParameter("javax.ws.rs.Application", appClass.getName()); final ServletContextHandler mainHandler = new ServletContextHandler(contexts, "/", true, false); mainHandler.addServlet(jaxrs, "/*"); server.setHandler(mainHandler); try { server.start(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } return server; }
protected String _calcName(Class<?> cls) { if (cls.isArray()) { return _calcName(cls.getComponentType()) + "[]"; } return cls.getName(); }
public JsonMappingException endOfInputException(Class<?> instClass) { return JsonMappingException.from( _parser, "Unexpected end-of-input when trying to deserialize a " + instClass.getName()); }
public JsonMappingException instantiationException(Class<?> instClass, String msg) { return JsonMappingException.from( _parser, "Can not construct instance of " + instClass.getName() + ", problem: " + msg); }
/** * Helper method for constructing instantiation exception for specified type, to indicate problem * with physically constructing instance of specified class (missing constructor, exception from * constructor) */ public JsonMappingException instantiationException(Class<?> instClass, Throwable t) { return JsonMappingException.from( _parser, "Can not construct instance of " + instClass.getName() + ", problem: " + t.getMessage(), t); }