
com.workday.autoparse.json.context.JsonParserSettingsBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of autoparse-json Show documentation
Show all versions of autoparse-json Show documentation
A java library built specifically for Android that uses code generation to parse JSON into custom objects in your project.
/*
* Copyright 2016 Workday, Inc.
*
* This software is available under the MIT license.
* Please see the LICENSE.txt file in this project.
*/
package com.workday.autoparse.json.context;
import com.workday.autoparse.json.annotations.JsonObject;
import com.workday.autoparse.json.annotations.JsonParserPartition;
import com.workday.autoparse.json.parser.JsonObjectParser;
import com.workday.autoparse.json.parser.JsonObjectParserTable;
import com.workday.autoparse.json.updater.InstanceUpdaterTable;
import com.workday.meta.ConcreteTypeNames;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author nathan.taylor
* @since 2014-10-29.
*/
public class JsonParserSettingsBuilder {
public static final String DEFAULT_INSTANCE_UPDATER_PACKAGE =
InstanceUpdaterTable.class.getPackage().getName();
public static final String DEFAULT_OBJECT_PARSER_PACKAGE =
JsonObjectParserTable.class.getPackage().getName();
private String discriminationName;
private JsonObjectParser> unknownObjectParser;
private Class> unknownObjectClass;
private List partitionPackages = new ArrayList<>();
public JsonParserSettingsBuilder withDiscriminationName(String discriminationName) {
this.discriminationName = discriminationName;
return this;
}
public JsonParserSettingsBuilder withUnknownObjectParser(JsonObjectParser
unknownObjectParser,
Class unknownObjectType) {
this.unknownObjectParser = unknownObjectParser;
this.unknownObjectClass = unknownObjectType;
return this;
}
/**
* Indicate which partitions the {@link JsonParserSettings} should use. If no partitions are
* indicated, the default partition is assumed. To include explicit partitions and the default
* partition, include {@link #DEFAULT_INSTANCE_UPDATER_PACKAGE} in the list.
*
* When the {@link JsonParserSettings} instance is created, a validation is performed to ensure
* that there are no name collisions (multiple classes mapping to the same discrimination name)
* among the partitions. If there are, an {@link IllegalArgumentException} is thrown.
*
* @param partitionPackages The fully qualified names of the packages that hold the partitions.
* These are packages annotated with {@literal@}{@link JsonParserPartition}.
*
* @return This JsonParserSettingsBuilder.
*/
public JsonParserSettingsBuilder withPartitions(String... partitionPackages) {
this.partitionPackages.addAll(Arrays.asList(partitionPackages));
return this;
}
/**
* You can use this method to set the unknown object parser if that parser was generated by
* Autoparse.
*
* @param unknownObjectClass The type of object that will be created when an unknown object is
* encountered in the JSON document. This class must be annotated with {@literal@}{@link
* JsonObject}.
*/
public JsonParserSettingsBuilder withUnknownObjectClass(Class> unknownObjectClass) {
this.unknownObjectClass = unknownObjectClass;
String parserName = ConcreteTypeNames.constructClassName(unknownObjectClass,
GeneratedClassNames.PARSER_SUFFIX);
try {
@SuppressWarnings("unchecked")
Class extends JsonObjectParser>> parserClass =
(Class extends JsonObjectParser>>) Class.forName(parserName);
Object instance = parserClass.getDeclaredField("INSTANCE").get(null);
if (!(instance instanceof JsonObjectParser)) {
throw new RuntimeException(
"Public constant INSTANCE must be of type JsonObjectParser");
}
unknownObjectParser = (JsonObjectParser>) instance;
} catch (ClassNotFoundException e) {
throw new RuntimeException("No JsonObjectParser found for class of type "
+ unknownObjectClass, e);
} catch (NoSuchFieldException e) {
throw new RuntimeException("Parser does not have static field named INSTANCE.", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Field INSTANCE is not public.", e);
} catch (NullPointerException e) {
throw new RuntimeException("Field INSTANCE is not static", e);
}
return this;
}
public JsonParserSettings build() {
return new JsonParserSettings(discriminationName,
unknownObjectParser,
unknownObjectClass,
partitionPackages);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy