com.alibaba.fastjson.JSONWriter Java Examples

The following examples show how to use com.alibaba.fastjson.JSONWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JSONFileRegionDataOutput.java    From addrparser with Apache License 2.0 6 votes vote down vote up
@Override
public void init() throws IOException {

    if (initialized) {
        return;
    }

    synchronized (this) {
        if (initialized) {
            return;
        }

        if (this.filename == null) {
            this.writer = new JSONWriter(_writer);
        } else {
            this.writer = new JSONWriter(new FileWriter(filename));
        }

        this.writer.config(SerializerFeature.WriteEnumUsingName, false);
        this.writer.config(SerializerFeature.SortField, false);
        this.writer.startArray();

        this.initialized = true;
    }
}
 
Example #2
Source File: JSONBinary.java    From clouddisk with MIT License 6 votes vote down vote up
public <T> void writeObjToDisk(final T obj) {
	if (StringUtils.isNotBlank(binaryFilename)) {
		final File file = new File(getBianaryFilenamePath());
		if (!file.exists()) {
			file.getParentFile().mkdirs();
		}	 
		try (JSONWriter jsonWriter=new JSONWriter(new FileWriter(file))){
			jsonWriter.startObject();
			jsonWriter.writeObject(obj);
			jsonWriter.endObject();
			jsonWriter.flush();
			LOGGER.info("文件 { " + file.getName() + " } 写入位置" + file.getAbsolutePath());
		} catch (IOException e) {
			LOGGER.error(ERROR,e);
		}
	}
}
 
Example #3
Source File: JSONBinary.java    From clouddisk with MIT License 6 votes vote down vote up
public <T> void writeListToDisk(final List<T> lists) {
	if (StringUtils.isNotBlank(binaryFilename)) {
		final File file = new File(getBianaryFilenamePath());
		if (!file.exists()) {
			file.getParentFile().mkdirs();
		}
		try (JSONWriter jsonWriter=new JSONWriter(new FileWriter(file))){
			jsonWriter.startArray();
			lists.forEach(jsonWriter::writeObject);
			jsonWriter.endArray();
			jsonWriter.flush();
		} catch (IOException e) {
			LOGGER.error(ERROR,e);
		}
	}
}
 
Example #4
Source File: DefaultApiViewWriter.java    From swagger with Apache License 2.0 5 votes vote down vote up
@Deprecated
@Override
public void writeApis(HttpServletRequest request, HttpServletResponse response, Properties props)
        throws Exception {
    String disabled = props.getProperty(Constants.DISABLED, "false");
    if (Boolean.TRUE.equals(Boolean.valueOf(disabled))) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    APIParseable restParser = APIParser.newInstance(props);
    response.setContentType("application/json;charset=utf-8");
    String devMode = props.getProperty("devMode");
    if (Boolean.valueOf(devMode)) {
        Object apis = restParser.parseAndNotStore();
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.writeObject(apis);
        writer.flush();
        writer.close();
    } else {
        if (!scanfed.get()) {
            restParser.parse();
            scanfed.set(true);
        }
        byte[] bs = Files.readAllBytes(Paths.get(props.getProperty("apiFile")));
        OutputStream out = response.getOutputStream();
        out.write(bs);
        out.flush();
        out.close();
    }
}
 
Example #5
Source File: APIParser.java    From swagger with Apache License 2.0 5 votes vote down vote up
@Override
public void parse() throws Exception {
    /* 将结果写入文件 */
    java.nio.file.Path f = Paths.get(file);
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.join("", "生成的文件保存在=>", f.toString()));
    }
    JSONWriter writer = new JSONWriter(Files.newBufferedWriter(f, StandardCharsets.UTF_8));
    writer.config(SerializerFeature.DisableCircularReferenceDetect, false);
    APIDoc api = (APIDoc) parseAndNotStore();
    writer.writeObject(api);
    writer.flush();
    writer.close();
}