Skip to content

Commit

Permalink
Use unchecked exceptions in JsonOutput
Browse files Browse the repository at this point in the history
* Retain the public API, but change the internals
  • Loading branch information
jodastephen committed Nov 7, 2024
1 parent c1b4459 commit 5f55bce
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 158 deletions.
34 changes: 20 additions & 14 deletions src/main/java/org/joda/beans/ser/json/JodaBeanJsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.joda.beans.ser.json;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -162,14 +163,18 @@ public void write(Bean bean, Appendable output) throws IOException {
public void write(Bean bean, boolean rootType, Appendable output) throws IOException {
JodaBeanUtils.notNull(bean, "bean");
JodaBeanUtils.notNull(output, "output");
this.output = new JsonOutput(output, settings.getIndent(), settings.getNewLine());
writeBean(bean, bean.getClass(), rootType ? RootType.ROOT_WITH_TYPE : RootType.ROOT_WITHOUT_TYPE);
output.append(settings.getNewLine());
try {
this.output = new JsonOutput(output, settings.getIndent(), settings.getNewLine());
writeBean(bean, bean.getClass(), rootType ? RootType.ROOT_WITH_TYPE : RootType.ROOT_WITHOUT_TYPE);
output.append(settings.getNewLine());
} catch (UncheckedIOException ex) {
throw ex.getCause();
}
}

//-----------------------------------------------------------------------
// write a bean as a JSON object
private void writeBean(Bean bean, Class<?> declaredType, RootType rootTypeFlag) throws IOException {
private void writeBean(Bean bean, Class<?> declaredType, RootType rootTypeFlag) {
output.writeObjectStart();
// type information
if (rootTypeFlag == RootType.ROOT_WITH_TYPE || (rootTypeFlag == RootType.NOT_ROOT && bean.getClass() != declaredType)) {
Expand Down Expand Up @@ -208,7 +213,7 @@ private void writeBean(Bean bean, Class<?> declaredType, RootType rootTypeFlag)

//-----------------------------------------------------------------------
// write a collection
private void writeElements(SerIterator itemIterator) throws IOException {
private void writeElements(SerIterator itemIterator) {
if (itemIterator.metaTypeRequired()) {
output.writeObjectStart();
output.writeObjectKeyValue(META, itemIterator.metaTypeName());
Expand All @@ -231,7 +236,7 @@ private void writeElements(SerIterator itemIterator) throws IOException {
}

// write list/set/array
private void writeArray(SerIterator itemIterator) throws IOException {
private void writeArray(SerIterator itemIterator) {
output.writeArrayStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -242,7 +247,7 @@ private void writeArray(SerIterator itemIterator) throws IOException {
}

// write map
private void writeMap(SerIterator itemIterator) throws IOException {
private void writeMap(SerIterator itemIterator) {
// if key type is known and convertible use short key format, else use full bean format
if (settings.getConverter().isConvertible(itemIterator.keyType())) {
writeMapSimple(itemIterator);
Expand All @@ -252,7 +257,7 @@ private void writeMap(SerIterator itemIterator) throws IOException {
}

// write map with simple keys
private void writeMapSimple(SerIterator itemIterator) throws IOException {
private void writeMapSimple(SerIterator itemIterator) {
var keyConverter = settings.getConverter().findConverterNoGenerics(itemIterator.keyType());
output.writeObjectStart();
while (itemIterator.hasNext()) {
Expand All @@ -272,7 +277,7 @@ private void writeMapSimple(SerIterator itemIterator) throws IOException {
}

// write map with complex keys
private void writeMapComplex(SerIterator itemIterator) throws IOException {
private void writeMapComplex(SerIterator itemIterator) {
output.writeArrayStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -292,7 +297,7 @@ private void writeMapComplex(SerIterator itemIterator) throws IOException {
}

// write table
private void writeTable(SerIterator itemIterator) throws IOException {
private void writeTable(SerIterator itemIterator) {
output.writeArrayStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -310,7 +315,7 @@ private void writeTable(SerIterator itemIterator) throws IOException {
}

// write grid using sparse approach
private void writeGrid(SerIterator itemIterator) throws IOException {
private void writeGrid(SerIterator itemIterator) {
output.writeArrayStart();
output.writeArrayItemStart();
output.writeInt(itemIterator.dimensionSize(0));
Expand All @@ -332,7 +337,7 @@ private void writeGrid(SerIterator itemIterator) throws IOException {
}

// write counted set
private void writeCounted(final SerIterator itemIterator) throws IOException {
private void writeCounted(final SerIterator itemIterator) {
output.writeArrayStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -348,7 +353,7 @@ private void writeCounted(final SerIterator itemIterator) throws IOException {
}

// write collection object
private void writeObject(Class<?> declaredType, Object obj, SerIterator parentIterator) throws IOException {
private void writeObject(Class<?> declaredType, Object obj, SerIterator parentIterator) {
if (obj == null) {
output.writeNull();
} else if (settings.getConverter().isConvertible(obj.getClass())) {
Expand All @@ -369,7 +374,7 @@ private void writeObject(Class<?> declaredType, Object obj, SerIterator parentIt

//-----------------------------------------------------------------------
// write simple type
private void writeSimple(Class<?> declaredType, Object value) throws IOException {
private void writeSimple(Class<?> declaredType, Object value) {
// simple types have no need to write a type object
Class<?> realType = value.getClass();
if (realType == Integer.class) {
Expand All @@ -381,6 +386,7 @@ private void writeSimple(Class<?> declaredType, Object value) throws IOException
output.writeDouble(dbl);
return;
}
System.out.println("");
} else if (realType == Boolean.class) {
output.writeBoolean(((Boolean) value).booleanValue());
return;
Expand Down
33 changes: 19 additions & 14 deletions src/main/java/org/joda/beans/ser/json/JodaBeanSimpleJsonWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.joda.beans.ser.json;

import java.io.IOException;
import java.io.UncheckedIOException;

import org.joda.beans.Bean;
import org.joda.beans.JodaBeanUtils;
Expand Down Expand Up @@ -96,14 +97,18 @@ public String write(Bean bean) {
public void write(Bean bean, Appendable output) throws IOException {
JodaBeanUtils.notNull(bean, "bean");
JodaBeanUtils.notNull(output, "output");
this.output = new JsonOutput(output, settings.getIndent(), settings.getNewLine());
writeBean(bean, bean.getClass());
output.append(settings.getNewLine());
try {
this.output = new JsonOutput(output, settings.getIndent(), settings.getNewLine());
writeBean(bean, bean.getClass());
output.append(settings.getNewLine());
} catch (UncheckedIOException ex) {
throw ex.getCause();
}
}

//-----------------------------------------------------------------------
// write a bean as a JSON object
private void writeBean(Bean bean, Class<?> declaredType) throws IOException {
private void writeBean(Bean bean, Class<?> declaredType) {
output.writeObjectStart();
// property information
for (MetaProperty<?> prop : bean.metaBean().metaPropertyIterable()) {
Expand Down Expand Up @@ -134,7 +139,7 @@ private void writeBean(Bean bean, Class<?> declaredType) throws IOException {

//-----------------------------------------------------------------------
// write a collection
private void writeElements(SerIterator itemIterator) throws IOException {
private void writeElements(SerIterator itemIterator) {
if (itemIterator.category() == SerCategory.MAP) {
writeMap(itemIterator);
} else if (itemIterator.category() == SerCategory.COUNTED) {
Expand All @@ -149,7 +154,7 @@ private void writeElements(SerIterator itemIterator) throws IOException {
}

// write list/set/array
private void writeArray(SerIterator itemIterator) throws IOException {
private void writeArray(SerIterator itemIterator) {
output.writeArrayStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -160,7 +165,7 @@ private void writeArray(SerIterator itemIterator) throws IOException {
}

// write map
private void writeMap(SerIterator itemIterator) throws IOException {
private void writeMap(SerIterator itemIterator) {
// if key type is known and convertible use short key format, else use full bean format
if (settings.getConverter().isConvertible(itemIterator.keyType())) {
writeMapSimple(itemIterator);
Expand All @@ -170,7 +175,7 @@ private void writeMap(SerIterator itemIterator) throws IOException {
}

// write map with simple keys
private void writeMapSimple(SerIterator itemIterator) throws IOException {
private void writeMapSimple(SerIterator itemIterator) {
var keyConverter = settings.getConverter().findConverterNoGenerics(itemIterator.keyType());
output.writeObjectStart();
while (itemIterator.hasNext()) {
Expand All @@ -190,7 +195,7 @@ private void writeMapSimple(SerIterator itemIterator) throws IOException {
}

// write map with complex keys
private void writeMapComplex(SerIterator itemIterator) throws IOException {
private void writeMapComplex(SerIterator itemIterator) {
output.writeObjectStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -209,7 +214,7 @@ private void writeMapComplex(SerIterator itemIterator) throws IOException {
}

// write table
private void writeTable(SerIterator itemIterator) throws IOException {
private void writeTable(SerIterator itemIterator) {
output.writeArrayStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -227,7 +232,7 @@ private void writeTable(SerIterator itemIterator) throws IOException {
}

// write grid using sparse approach
private void writeGrid(SerIterator itemIterator) throws IOException {
private void writeGrid(SerIterator itemIterator) {
output.writeArrayStart();
output.writeArrayItemStart();
output.writeInt(itemIterator.dimensionSize(0));
Expand All @@ -249,7 +254,7 @@ private void writeGrid(SerIterator itemIterator) throws IOException {
}

// write counted set
private void writeCounted(final SerIterator itemIterator) throws IOException {
private void writeCounted(final SerIterator itemIterator) {
output.writeArrayStart();
while (itemIterator.hasNext()) {
itemIterator.next();
Expand All @@ -265,7 +270,7 @@ private void writeCounted(final SerIterator itemIterator) throws IOException {
}

// write collection object
private void writeObject(Class<?> declaredType, Object obj, SerIterator parentIterator) throws IOException {
private void writeObject(Class<?> declaredType, Object obj, SerIterator parentIterator) {
if (obj == null) {
output.writeNull();
} else if (settings.getConverter().isConvertible(obj.getClass())) {
Expand All @@ -286,7 +291,7 @@ private void writeObject(Class<?> declaredType, Object obj, SerIterator parentIt

//-----------------------------------------------------------------------
// write simple type
private void writeSimple(Class<?> declaredType, Object value) throws IOException {
private void writeSimple(Class<?> declaredType, Object value) {
Class<?> realType = value.getClass();
if (realType == Integer.class) {
output.writeInt(((Integer) value).intValue());
Expand Down
Loading

0 comments on commit 5f55bce

Please sign in to comment.