/** * Same as {@link #parse(Object, Type, Type...)} except the type has already been converted into a {@link ClassMeta} * object. * * <p> * This is mostly an internal method used by the framework. * * @param <T> The class type of the object being created. * @param input * The input. * See {@link #parse(Object, Type, Type...)} for details. * @param type The object type to create. * @return The parsed object. * @throws ParseException * If the input contains a syntax error or is malformed, or is not valid for the specified type. */ public final <T> T parse(Object input, ClassMeta<T> type) throws ParseException { return createSession().parse(input, type); }
if (p != null) { try { if (p.isReaderParser()) return p.parse(new InputStreamReader(is, IOUtils.UTF8), c); return p.parse(is, c); } catch (ParseException e) { throw new ServletException("Could not parse resource '' as media type '"+mediaType+"'.");
/** * Returns <jk>true</jk> if this parser can handle the specified content type. * * @param contentType The content type to test. * @return <jk>true</jk> if this parser can handle the specified content type. */ public boolean canHandle(String contentType) { if (contentType != null) for (MediaType mt : getMediaTypes()) if (contentType.equals(mt.toString())) return true; return false; }
@Override /* Context */ public ParserSession createSession() { return createSession(createDefaultSessionArgs()); }
/** * Construct a JSON array directly from text using the specified parser. * * @param s The string being parsed. * @param p The parser to use to parse the input. * @throws ParseException If the input contains a syntax error or is malformed. */ public ObjectList(CharSequence s, Parser p) throws ParseException { this(p == null ? null : p.createBeanSession()); if (p == null) p = JsonParser.DEFAULT; if (s != null) p.parseIntoCollection(s, this, bs().object()); }
@Test public void testXMLGregorianCalendar() throws Exception { if (isValidationOnly()) return; GregorianCalendar gc = new GregorianCalendar(); XMLGregorianCalendar c = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc); Serializer s = getSerializer().builder().pojoSwaps(XMLGregorianCalendarSwap.class).build(); Parser p = getParser().builder().pojoSwaps(XMLGregorianCalendarSwap.class).build(); Object r = s.serialize(c); XMLGregorianCalendar c2 = p.parse(r, XMLGregorianCalendar.class); assertEquals(c, c2); }
if (parser == null || (mt.toString().equals("text/plain") && ! parser.canHandle(ct))) { if (type.hasStringTransform()) return type.getStringTransform().transform(getResponseAsString()); try (Closeable in = parser.isReaderParser() ? getReader() : getInputStream()) { return parser.createSession(pArgs).parse(in, type); getResponseHeader("Content-Type"), parser == null ? null : parser.getMediaTypes() );
public <T> T roundTrip(T object, Serializer serializer, Parser parser) throws Exception { Object out = serialize(object, serializer); if (parser == null) return object; T o = (T)parser.parse(out, object == null ? Object.class : object.getClass()); return (returnOriginalObject ? object : o); }
@Override protected Parser applySettings(Parser p) throws Exception { return p.builder().build(); }
/** * Returns the reader parser matching the request <code>Accept</code> header. * * @return The reader parser matching the request <code>Accept</code> header, or <jk>null</jk> * if no matching reader parser was found, or the matching parser was an input stream parser. */ public ReaderParser getReaderParser() { Parser p = getParser(); if (p.isReaderParser()) return (ReaderParser)p; return null; }
sgb = SerializerGroup.create(); pgb = ParserGroup.create(); uepb = Parser.create(); bcb = beanContext.builder(); sgb.append(mSerializers); if (uepb != null && partParser instanceof Parser) { Parser pp = (Parser)partParser; partParser = (HttpPartParser)pp.builder().apply(uepb.getPropertyStore()).build();
@Override /* MessageBodyReader */ public Object readFrom(Class<Object> type, Type gType, Annotation[] a, MediaType mediaType, MultivaluedMap<String,String> headers, InputStream in) throws IOException, WebApplicationException { try { ParserMatch pm = parsers.getParserMatch(mediaType.toString()); if (pm == null) throw new WebApplicationException(SC_UNSUPPORTED_MEDIA_TYPE); Parser p = pm.getParser(); ObjectMap mp = getMethodProperties(a); mp.put("mediaType", mediaType.toString()); Locale locale = getLocale(headers); TimeZone timeZone = getTimeZone(headers); ParserSession session = p.createSession(new ParserSessionArgs(mp, null, locale, timeZone, pm.getMediaType(), null, null, null)); Object in2 = session.isReaderParser() ? new InputStreamReader(in, UTF8) : in; return session.parse(in2, p.getClassMeta(gType)); } catch (ParseException e) { throw new IOException(e); } }
@Override /* Context */ public final ParserSessionArgs createDefaultSessionArgs() { return new ParserSessionArgs().mediaType(getPrimaryMediaType()); }
@Override /* MessageBodyReader */ public Object readFrom(Class<Object> type, Type gType, Annotation[] a, MediaType mediaType, MultivaluedMap<String,String> headers, InputStream in) throws IOException, WebApplicationException { try { String mt = parsers.findMatch(mediaType.toString()); if (mt == null) throw new WebApplicationException(SC_UNSUPPORTED_MEDIA_TYPE); Parser p = parsers.getParser(mt); BeanContext bc = p.getBeanContext(); ClassMeta<?> cm = bc.getClassMeta(gType); ObjectMap mp = getMethodProperties(a); mp.put("mediaType", mediaType.toString()); if (p.isReaderParser()) { ReaderParser p2 = (ReaderParser)p; InputStreamReader r = new InputStreamReader(in, IOUtils.UTF8); ParserSession session = p2.createSession(r, mp, null, null); return p2.parse(session, cm); } InputStreamParser p2 = (InputStreamParser)p; ParserSession session = p2.createSession(in, mp, null, null); return p2.parse(session, cm); } catch (ParseException e) { throw new IOException(e); } } }
/** * Construct an ObjectMap directly from a string using the specified parser. * * @param s The string being parsed. * @param p The parser to use to parse the input. * @throws ParseException If the input contains a syntax error or is malformed. */ public ObjectMap(CharSequence s, Parser p) throws ParseException { this(p == null ? null : p.createBeanSession()); if (p == null) p = JsonParser.DEFAULT; if (! StringUtils.isEmpty(s)) p.parseIntoMap(s, this, bs().string(), bs().object()); }
@Override /* Context */ public ObjectMap asMap() { return super.asMap() .append("InputStreamParser", new ObjectMap() .append("binaryFormat", binaryFormat) ); } }
/** * Construct an ObjectMap directly from a reader using the specified parser. * * @param r The reader to read from. The reader will be wrapped in a {@link BufferedReader} if it isn't already. * @param p The parser to use to parse the input. * @throws ParseException If the input contains a syntax error or is malformed. * @throws IOException If a problem occurred trying to read from the reader. */ public ObjectMap(Reader r, Parser p) throws ParseException, IOException { this(p == null ? null : p.createBeanSession()); parseReader(r, p); }
Parser p2 = p.builder().trimStrings().build(); a = p.parse(s2.serialize(in), String.class); assertEqualObjects(e, a); a = p2.parse(s.serialize(in), String.class); assertEqualObjects(e, a); a = p.parse(s2.serialize(in), ObjectMap.class); assertEqualObjects(e, a); a = p2.parse(s.serialize(in), ObjectMap.class); assertEqualObjects(e, a); a = p.parse(s2.serialize(in), ObjectList.class); assertEqualObjects(e, a); a = p2.parse(s.serialize(in), ObjectList.class); assertEqualObjects(e, a); a = p.parse(s2.serialize(in), A.class); assertEqualObjects(e, a); a = p2.parse(s.serialize(in), A.class); assertEqualObjects(e, a);
if (parser == null || (mt.toString().equals("text/plain") && ! parser.canHandle(ct))) { if (type.hasStringTransform()) return type.getStringTransform().transform(getResponseAsString()); try (Closeable in = parser.isReaderParser() ? getReader() : getInputStream()) { return parser.createSession(pArgs).parse(in, type); getResponseHeader("Content-Type"), parser == null ? null : parser.getMediaTypes() );
public <T> T roundTrip(T object, Type c, Type...args) throws Exception { Object out = serialize(object, this.s); if (p == null) return object; T o = (T)this.p.parse(out, c, args); return (returnOriginalObject ? object : o); }