/*
 * Copyright 2015-2017 the original author or authors.
 *
 * 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 org.lastaflute.core.json;

import java.lang.reflect.ParameterizedType;

import org.dbflute.optional.OptionalThing;
import org.lastaflute.core.json.engine.RealJsonEngine;

/**
 * @author jflute
 */
public interface JsonManager {

    /**
     * Convert from the JSON string to the specified bean.
     * @param <BEAN> The type of JSON bean.
     * @param json The string of JSON to be parsed. (NotNull, EmptyAllowed: returns new-only)
     * @param beanType The type of bean to convert, should have default constructor. (NotNull)
     * @return The new-created bean that has the JSON values. (NotNull: if empty JSON, new-only)
     */
    <BEAN> BEAN fromJson(String json, Class<BEAN> beanType);

    /**
     * Convert from the JSON string to the parameterized bean.
     * You need to specify parameterized type like this:
     * <pre>
     * List&lt;SeaBean&gt; <span style="color: #553000">seaList</span> = fromJsonParameteried(<span style="color: #553000">json</span>, new ParameterizedRef<span style="color: #994747">&lt;List&lt;SeaBean&gt;&gt;</span>(){
     * }.getType());
     * 
     * MaihamaBean&lt;SeaBean&gt; maihama = fromJsonParameteried(<span style="color: #553000">json</span>, new ParameterizedRef<span style="color: #994747">&lt;MaihamaBean&lt;SeaBean&gt;&gt;</span>() {
     * }.getType());
     * </pre>
     * @param <BEAN> The type of JSON bean as root.
     * @param json The string of JSON to be parsed. (NotNull, EmptyAllowed: returns new-only)
     * @param parameterizedType The parameterized type of bean to convert, should have default constructor. (NotNull)
     * @return The new-created bean that has the JSON values, also List and Map. (NotNull: if empty JSON, new-only)
     */
    <BEAN> BEAN fromJsonParameteried(String json, ParameterizedType parameterizedType);

    /**
     * Convert from the source object to JSON string.
     * @param bean The instance of bean to encode. (NotNull)
     * @return The encoded JSON string. (NotNull)
     */
    String toJson(Object bean);

    /**
     * Create new JSON engine as another rule. <br>
     * (inheriting basic settings e.g. serializeNulls, yourCollections)
     * @param mappingOption The optional option of (NotNull, EmptyAllowed: when default options)
     * @return The JSON engine as another rule. (NotNull)
     */
    RealJsonEngine newAnotherEngine(OptionalThing<JsonMappingOption> mappingOption);
}
/*
 * Copyright 2015-2017 the original author or authors.
 *
 * 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 org.lastaflute.core.json;

import java.lang.reflect.ParameterizedType;

import org.dbflute.optional.OptionalThing;
import org.lastaflute.core.json.engine.RealJsonEngine;

/**
 * @author jflute
 */
public interface JsonManager {

    /**
     * Convert from the JSON string to the specified bean.
     * @param <BEAN> The type of JSON bean.
     * @param json The string of JSON to be parsed. (NotNull, EmptyAllowed: returns new-only)
     * @param beanType The type of bean to convert, should have default constructor. (NotNull)
     * @return The new-created bean that has the JSON values. (NotNull: if empty JSON, new-only)
     */
    <BEAN> BEAN fromJson(String json, Class<BEAN> beanType);

    /**
     * Convert from the JSON string to the parameterized bean.
     * You need to specify parameterized type like this:
     * <pre>
     * List&lt;SeaBean&gt; <span style="color: #553000">seaList</span> = fromJsonParameteried(<span style="color: #553000">json</span>, new ParameterizedRef<span style="color: #994747">&lt;List&lt;SeaBean&gt;&gt;</span>(){
     * }.getType());
     * 
     * MaihamaBean&lt;SeaBean&gt; maihama = fromJsonParameteried(<span style="color: #553000">json</span>, new ParameterizedRef<span style="color: #994747">&lt;MaihamaBean&lt;SeaBean&gt;&gt;</span>() {
     * }.getType());
     * </pre>
     * @param <BEAN> The type of JSON bean as root.
     * @param json The string of JSON to be parsed. (NotNull, EmptyAllowed: returns new-only)
     * @param parameterizedType The parameterized type of bean to convert, should have default constructor. (NotNull)
     * @return The new-created bean that has the JSON values, also List and Map. (NotNull: if empty JSON, new-only)
     */
    <BEAN> BEAN fromJsonParameteried(String json, ParameterizedType parameterizedType);

    /**
     * Convert from the source object to JSON string.
     * @param bean The instance of bean to encode. (NotNull)
     * @return The encoded JSON string. (NotNull)
     */
    String toJson(Object bean);

    /**
     * Create new JSON engine as another rule. <br>
     * (inheriting basic settings e.g. serializeNulls, yourCollections)
     * @param mappingOption The optional option for new another engine. (NotNull, EmptyAllowed: when default options)
     * @return The JSON engine as another rule. (NotNull)
     */
    RealJsonEngine newAnotherEngine(OptionalThing<JsonMappingOption> mappingOption);
}
improve javadoc of newAnotherEngine()