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

com.holonplatform.json.gson.jaxrs.internal.GsonContextResolver Maven / Gradle / Ivy

/*
 * Copyright 2016-2017 Axioma srl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.holonplatform.json.gson.jaxrs.internal;

import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.holonplatform.core.Context;
import com.holonplatform.core.property.PropertyBox;
import com.holonplatform.json.gson.GsonConfiguration;

/**
 * JAX-RS {@link ContextResolver} to replace default {@link GsonBuilder} for
 * Gson marshalling using a builder with {@link PropertyBox} handling
 * capabilities.
 * 
 * @since 5.0.0
 */
@Produces(MediaType.APPLICATION_JSON)
public class GsonContextResolver implements ContextResolver {

	/**
	 * Default GsonBuilder
	 */
	private final GsonBuilder builder;

	/**
	 * Gson instance
	 */
	private Gson gson;

	/**
	 * Constructor
	 * @param prettyPrint true to enable pretty printing for
	 *                    serialized JSON
	 */
	public GsonContextResolver(boolean prettyPrint) {
		super();
		this.builder = GsonConfiguration.builder();
		if (prettyPrint) {
			builder.setPrettyPrinting();
		}
	}

	/**
	 * Get the default {@link Gson} instance.
	 * @return The {@link Gson} instance
	 */
	private Gson getDefaultGson() {
		if (gson == null) {
			gson = builder.create();
		}
		return gson;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.ws.rs.ext.ContextResolver#getContext(java.lang.Class)
	 */
	@Override
	public Gson getContext(Class type) {
		return Context.get().resource(Gson.class.getName(), Gson.class).orElseGet(this::getDefaultGson);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy