net.ymate.platform.webmvc.view.impl.JsonView.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.webmvc.view.impl.JsonView.java

Source

/*
 * Copyright 2007-2016 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 net.ymate.platform.webmvc.view.impl;

import com.alibaba.fastjson.JSON;
import net.ymate.platform.webmvc.base.Type;
import net.ymate.platform.webmvc.context.WebContext;
import net.ymate.platform.webmvc.view.AbstractView;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletResponse;

/**
 * JSON
 *
 * @author  (suninformation@163.com) on 2011-10-23 ?11:27:16
 * @version 1.0
 */
public class JsonView extends AbstractView {

    protected Object __jsonObj;
    protected boolean __withContentType;
    protected String __jsonCallback;

    public static JsonView bind(Object obj) {
        if (obj instanceof String) {
            return new JsonView((String) obj);
        } else {
            return new JsonView(obj);
        }
    }

    /**
     * 
     *
     * @param obj Java
     */
    public JsonView(Object obj) {
        __jsonObj = JSON.toJSON(obj);
    }

    /**
     * 
     *
     * @param jsonStr JSON
     */
    public JsonView(String jsonStr) {
        __jsonObj = JSON.parse(jsonStr);
    }

    /**
     * @return ContentType"application/json""text/javascript"
     */
    public JsonView withContentType() {
        __withContentType = true;
        return this;
    }

    /**
     * @param callback ??
     * @return JSONP????callbackcallback???
     */
    public JsonView withJsonCallback(String callback) {
        __jsonCallback = StringUtils.defaultIfEmpty(callback, null);
        return this;
    }

    protected void __doRenderView() throws Exception {
        HttpServletResponse _response = WebContext.getResponse();
        if (StringUtils.isNotBlank(getContentType())) {
            _response.setContentType(getContentType());
        } else if (this.__withContentType) {
            if (__jsonCallback == null) {
                _response.setContentType(Type.ContentType.JSON.getContentType());
            } else {
                _response.setContentType(Type.ContentType.JAVASCRIPT.getContentType());
            }
        }
        StringBuilder _jsonStr = new StringBuilder(__jsonObj.toString());
        if (__jsonCallback != null) {
            _jsonStr.insert(0, __jsonCallback + "(").append(");");
        }
        IOUtils.write(_jsonStr.toString(), _response.getOutputStream(), _response.getCharacterEncoding());
    }
}