List of usage examples for com.fasterxml.jackson.core JsonGenerator copyCurrentStructure
public abstract void copyCurrentStructure(JsonParser jp) throws IOException, JsonProcessingException;
From source file:com.bazaarvoice.jsonpps.PrettyPrintJson.java
private void copyCurrentStructure(JsonParser parser, ObjectMapper mapper, int depth, JsonGenerator generator) throws IOException { // Avoid using the mapper to parse the entire input until we absolutely must. This allows pretty // printing huge top-level arrays (that wouldn't fit in memory) containing smaller objects (that // individually do fit in memory) where the objects are printed with sorted keys. JsonToken t = parser.getCurrentToken(); if (t == null) { generator.copyCurrentStructure(parser); // Will report the error of a null token. return;/* w w w .ja v a2 s .c om*/ } int id = t.id(); if (id == ID_FIELD_NAME) { if (depth > flatten) { generator.writeFieldName(parser.getCurrentName()); } t = parser.nextToken(); id = t.id(); } switch (id) { case ID_START_OBJECT: if (sortKeys && depth >= flatten) { // Load the entire object in memory so we can sort its keys and serialize it back out. mapper.writeValue(generator, parser.readValueAs(Map.class)); } else { // Don't load the whole object into memory. Copy it in a memory-efficient streaming fashion. if (depth >= flatten) { generator.writeStartObject(); } while (parser.nextToken() != JsonToken.END_OBJECT) { copyCurrentStructure(parser, mapper, depth + 1, generator); } if (depth >= flatten) { generator.writeEndObject(); } } break; case ID_START_ARRAY: // Don't load the whole array into memory. Copy it in a memory-efficient streaming fashion. if (depth >= flatten) { generator.writeStartArray(); } while (parser.nextToken() != JsonToken.END_ARRAY) { copyCurrentStructure(parser, mapper, depth + 1, generator); } if (depth >= flatten) { generator.writeEndArray(); } break; default: generator.copyCurrentEvent(parser); break; } }
From source file:org.usrz.libs.webtools.resources.ServeResource.java
private Response produce(String path) throws Exception { /* Basic check for null/empty path */ if ((path == null) || (path.length() == 0)) return NOT_FOUND; /* Get our resource file, potentially a ".less" file for CSS */ Resource resource = manager.getResource(path); if ((resource == null) && path.endsWith(".css")) { path = path.substring(0, path.length() - 4) + ".less"; resource = manager.getResource(path); }//from ww w .ja v a 2s .c om /* If the root is incorrect, log this, if not found, 404 it! */ if (resource == null) return NOT_FOUND; /* Ok, we have a resource on disk, this can be potentially long ... */ final String fileName = resource.getFile().getName(); /* Check and validated our cache */ Entry cached = cache.computeIfPresent(resource, (r, entry) -> entry.resource.hasChanged() ? null : entry); /* If we have no cache, we *might* want to cache something */ if (cached == null) { /* What to do, what to do? */ if ((fileName.endsWith(".css") && minify) || fileName.endsWith(".less")) { /* Lessify CSS and cache */ xlog.debug("Lessifying resource \"%s\"", fileName); cached = new Entry(resource, lxess.convert(resource, minify), styleMediaType); } else if (fileName.endsWith(".js") && minify) { /* Uglify JavaScript and cache */ xlog.debug("Uglifying resource \"%s\"", fileName); cached = new Entry(resource, uglify.convert(resource.readString(), minify, minify), scriptMediaType); } else if (fileName.endsWith(".json")) { /* Strip comments and normalize JSON */ xlog.debug("Normalizing JSON resource \"%s\"", fileName); /* All to do with Jackson */ final Reader reader = resource.read(); final StringWriter writer = new StringWriter(); final JsonParser parser = json.createParser(reader); final JsonGenerator generator = json.createGenerator(writer); /* Not minifying? Means pretty printing! */ if (!minify) generator.useDefaultPrettyPrinter(); /* Get our schtuff through the pipeline */ parser.nextToken(); generator.copyCurrentStructure(parser); generator.flush(); generator.close(); reader.close(); parser.close(); /* Cached results... */ cached = new Entry(resource, writer.toString(), jsonMediaType); } /* Do we have anything to cache? */ if (cached != null) { xlog.debug("Caching resource \"%s\"", fileName); cache.put(resource, cached); } } /* Prepare our basic response from either cache or file */ final ResponseBuilder response = Response.ok(); if (cached != null) { /* Response from cache */ xlog.trace("Serving cached resource \"%s\"", fileName); response.entity(cached.contents).lastModified(new Date(resource.lastModifiedAt())).type(cached.type); } else { /* Response from a file */ xlog.trace("Serving file-based resource \"%s\"", fileName); /* If text/* or application/javascript, append encoding */ MediaType type = MediaTypes.get(fileName); if (type.getType().equals("text") || scriptMediaType.isCompatible(type)) { type = type.withCharset(charsetName); } /* Our file is served! */ response.entity(resource.getFile()).lastModified(new Date(resource.lastModifiedAt())).type(type); } /* Caching headers and build response */ final Date expires = Date.from(Instant.now().plus(cacheDuration)); return response.cacheControl(cacheControl).expires(expires).build(); }