package ezvcard.io.json; import java.io.Flushable; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.PrettyPrinter; import ezvcard.VCard; import ezvcard.VCardDataType; import ezvcard.VCardVersion; import ezvcard.io.EmbeddedVCardException; import ezvcard.io.SkipMeException; import ezvcard.io.StreamWriter; import ezvcard.io.scribe.VCardPropertyScribe; import ezvcard.parameter.VCardParameters; import ezvcard.property.VCardProperty; /* Copyright (c) 2012-2023, Michael Angstadt All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. */ /** *
* Writes {@link VCard} objects to a JSON data stream (jCard format). *
** Example: *
* *
* VCard vcard1 = ...
* VCard vcard2 = ...
* Path file = Paths.get("vcard.json");
* try (JCardWriter writer = new JCardWriter(file)) {
* writer.write(vcard1);
* writer.write(vcard2);
* }
*
* @author Michael Angstadt
* @author Buddy Gorven
* @see RFC 7095
*/
public class JCardWriter extends StreamWriter implements Flushable {
private final JCardRawWriter writer;
private final VCardVersion targetVersion = VCardVersion.V4_0;
private JsonGenerator generator = null;
/**
* @param out the output stream to write to (UTF-8 encoding will be used)
*/
public JCardWriter(OutputStream out) {
this(new OutputStreamWriter(out, StandardCharsets.UTF_8));
}
/**
* @param out the output stream to write to (UTF-8 encoding will be used)
* @param wrapInArray true to enclose all written vCards in a JSON array,
* false not to
*/
public JCardWriter(OutputStream out, boolean wrapInArray) {
this(new OutputStreamWriter(out, StandardCharsets.UTF_8), wrapInArray);
}
/**
* @param file the file to write to (UTF-8 encoding will be used)
* @throws IOException if there's a problem opening the file
*/
public JCardWriter(Path file) throws IOException {
this(Files.newBufferedWriter(file, StandardCharsets.UTF_8));
}
/**
* @param file the file to write to (UTF-8 encoding will be used)
* @param wrapInArray true to enclose all written vCards in a JSON array,
* false not to
* @throws IOException if there's a problem opening the file
*/
public JCardWriter(Path file, boolean wrapInArray) throws IOException {
this(Files.newBufferedWriter(file, StandardCharsets.UTF_8), wrapInArray);
}
/**
* @param writer the writer to write to
*/
public JCardWriter(Writer writer) {
this(writer, false);
}
/**
* @param writer the writer to write to
* @param wrapInArray true to enclose all written vCards in a JSON array,
* false not to
*/
public JCardWriter(Writer writer, boolean wrapInArray) {
this.writer = new JCardRawWriter(writer, wrapInArray);
}
/**
* @param generator the generator to write to
*/
public JCardWriter(JsonGenerator generator) {
this.generator = generator;
this.writer = new JCardRawWriter(generator);
}
/**
* Writes a vCard to the stream.
* @param vcard the vCard that is being written
* @param properties the properties to write
* @throws IOException if there's a problem writing to the output stream
* @throws IllegalArgumentException if a scribe hasn't been registered for a
* custom property class (see: {@link #registerScribe registerScribe})
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void _write(VCard vcard, List