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

com.sun.faces.util.Json Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package com.sun.faces.util;

import static com.sun.faces.util.Json.Option.SKIP_NULL_VALUES;
import static com.sun.faces.util.Json.Option.USE_RFC1123_DATE;
import static java.time.ZoneOffset.UTC;
import static java.time.format.DateTimeFormatter.ISO_INSTANT;
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
import static java.util.Arrays.asList;
import static java.util.EnumSet.copyOf;
import static java.util.EnumSet.noneOf;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Date;
import java.util.EnumSet;
import java.util.Map;
import java.util.Map.Entry;

import jakarta.json.stream.JsonGenerator;

/**
 * 

* Generic JSON encoder using jakarta.json API. *

* This supports the standard types {@link Boolean}, {@link Number}, {@link Character}, {@link CharSequence}, * {@link Date}, {@link LocalDate} and {@link Instant}. If the given object type does not match any of them, then it * will attempt to inspect the object as a JavaBean using the {@link Introspector}, whereby the public properties * (public getters) will be encoded as a JS object. It also supports arrays, {@link Collection}s and {@link Map}s of * them, even nested ones. The dates are formatted as ISO8601 instant via {@link DateTimeFormatter#ISO_INSTANT}, so you * can if necessary just pass the value straight to new Date(value) in JavaScript. *

* Below encoding options are available: *

    *
  • {@link Option#SKIP_NULL_VALUES}: skip null values in arrays, collections, maps and beans. This may reduce an * unnecessarily bloated JSON object. *
  • {@link Option#USE_RFC1123_DATE}: format dates as RFC1123 via {@link DateTimeFormatter#RFC_1123_DATE_TIME}. This * may improve compatibility with older web browsers. *
* * @author Bauke Scholtz * @since 2.3 */ public class Json { private static final String ERROR_INVALID_BEAN = "Cannot introspect object of type '%s' as bean."; private static final String ERROR_INVALID_GETTER = "Cannot invoke getter of property '%s' of bean '%s'."; public enum Option { /** * Skip null values in arrays, collections, maps and beans. This may reduce an unnecessarily bloated JSON object. */ SKIP_NULL_VALUES, /** * Format dates as RFC1123 via {@link DateTimeFormatter#RFC_1123_DATE_TIME}. This may improve compatibility with older * web browsers. */ USE_RFC1123_DATE; } /** * Encodes the given object as JSON and returns a string in JSON format. The encoded object will be available as * data property of the JS object in the returned JSON string. * * @param object The object to be encoded as JSON. * @param options The encoding options. * @return The JSON-encoded representation of the given object. * @throws IllegalArgumentException When given object or one of its properties cannot be inspected as a JavaBean. */ public static String encode(Object object, Option... options) { StringWriter writer = new StringWriter(); encode(object, writer, options); return writer.toString(); } /** * Encodes the given object as JSON while streaming the string in JSON format to the given writer. The encoded object * will be available as data property of the JS object in the returned JSON string. * * @param object The object to be encoded as JSON. * @param writer The writer to stream the encoded output to. * @param options The encoding options. * @throws IllegalArgumentException When given object or one of its properties cannot be inspected as a JavaBean. */ public static void encode(Object object, Writer writer, Option... options) { try (JsonGenerator generator = jakarta.json.Json.createGenerator(writer)) { generator.writeStartObject(); encode("data", object, generator, options.length == 0 ? noneOf(Option.class) : copyOf(asList(options))); generator.writeEnd(); } } private static void encode(String name, Object object, JsonGenerator generator, EnumSet




© 2015 - 2024 Weber Informatics LLC | Privacy Policy