com.getperka.flatpack.Packer Maven / Gradle / Ivy
/*
* #%L
* FlatPack serialization code
* %%
* Copyright (C) 2012 Perka Inc.
* %%
* 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.
* #L%
*/
package com.getperka.flatpack;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.validation.ConstraintViolation;
import com.getperka.flatpack.codexes.EntityCodex;
import com.getperka.flatpack.ext.Codex;
import com.getperka.flatpack.ext.SerializationContext;
import com.getperka.flatpack.ext.TypeContext;
import com.getperka.flatpack.util.FlatPackCollections;
import com.google.gson.stream.JsonWriter;
/**
* Writes {@link FlatPackEntity} objects into a {@link Writer}.
*/
public class Packer {
private final Configuration configuration;
private final TypeContext typeContext;
Packer(Configuration configuration, TypeContext typeContext) {
this.configuration = configuration;
this.typeContext = typeContext;
}
/**
* Write the given entity into a {@link Writer}.
*
* @param entity the entity to write
* @param out the destination output which will be closed by this method
*/
public void pack(FlatPackEntity> entity, Writer out) throws IOException {
SerializationContext context;
StringWriter verboseWriter = null;
{
Writer target;
if (configuration.isVerbose()) {
verboseWriter = new StringWriter();
target = verboseWriter;
} else {
target = out;
}
context = new SerializationContext(entity, configuration, typeContext, target);
}
@SuppressWarnings("unchecked")
Codex