All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fge.jsonschema.util.JacksonUtils Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
/*
 * Copyright (c) 2013, Francis Galiegue 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.github.fge.jsonschema.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;

import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;

/**
 * Utility class for Jackson
 *
 * 

This class provides a custom {@link JsonNodeFactory} and {@link * ObjectReader} which you should use preferably to your own (in particular, * the reader ensures that decimal values are read using {@link BigDecimal}.

*/ public final class JacksonUtils { private static final JsonNodeFactory FACTORY = JsonNodeFactory.withExactBigDecimals(false); private static final ObjectReader READER; private static final ObjectWriter WRITER; static { final ObjectMapper mapper = new ObjectMapper().setNodeFactory(FACTORY) .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); READER = mapper.reader(); WRITER = mapper.writerWithDefaultPrettyPrinter(); } private JacksonUtils() { } /** * Return a preconfigured {@link ObjectReader} to read JSON inputs * * @return the reader */ public static ObjectReader getReader() { return READER; } /** * Return a preconfigured {@link JsonNodeFactory} to generate JSON data as * {@link JsonNode}s * * @return the factory */ public static JsonNodeFactory nodeFactory() { return FACTORY; } /** * Return a map out of an object's members * *

If the node given as an argument is not a map, an empty map is * returned.

* * @param node the node * @return a map */ public static Map asMap(final JsonNode node) { if (!node.isObject()) return Collections.emptyMap(); final Iterator> iterator = node.fields(); final Map ret = Maps.newHashMap(); Map.Entry entry; while (iterator.hasNext()) { entry = iterator.next(); ret.put(entry.getKey(), entry.getValue()); } return ret; } /** * Pretty print a JSON value * * @param node the JSON value to print * @return the pretty printed value as a string */ public static String prettyPrint(final JsonNode node) { final StringWriter writer = new StringWriter(); try { WRITER.writeValue(writer, node); writer.flush(); } catch (IOException ignored) { // cannot happen } finally { Closeables.closeQuietly(writer); } return writer.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy