Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2019 Red Hat
*
* 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.
*/
package io.apicurio.datamodels.compat;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.SimpleType;
import io.apicurio.datamodels.cmd.ICommand;
import io.apicurio.datamodels.cmd.commands.CommandFactory;
import io.apicurio.datamodels.cmd.models.SimplifiedParameterType;
import io.apicurio.datamodels.cmd.models.SimplifiedPropertyType;
import io.apicurio.datamodels.cmd.models.SimplifiedType;
import io.apicurio.datamodels.cmd.util.ModelUtils;
import io.apicurio.datamodels.core.Constants;
import io.apicurio.datamodels.core.models.NodePath;
/**
* Compatibility layer for marshalling and unmarshalling commands.
* @author [email protected]
*/
public class MarshallCompat {
private static final ObjectMapper mapper = new ObjectMapper();
static {
SimpleModule module = new SimpleModule();
module.addSerializer(NodePath.class, new NodePathSerializer());
module.addSerializer(SimplifiedType.class, new SimplifiedTypeSerializer(SerializerFlavor.base));
module.addSerializer(SimplifiedParameterType.class, new SimplifiedTypeSerializer(SerializerFlavor.parameter));
module.addSerializer(SimplifiedPropertyType.class, new SimplifiedTypeSerializer(SerializerFlavor.property));
module.addDeserializer(NodePath.class, new NodePathDeserializer());
module.addDeserializer(SimplifiedType.class, new SimplifiedTypeDeserializer(SerializerFlavor.base));
module.addDeserializer(SimplifiedParameterType.class, new SimplifiedTypeDeserializer(SerializerFlavor.parameter));
module.addDeserializer(SimplifiedPropertyType.class, new SimplifiedTypeDeserializer(SerializerFlavor.property ));
mapper.registerModule(module);
}
/**
* Marshal a command to a JS object.
* @param command
*/
public static Object marshallCommand(ICommand command) {
JsonNode rval = mapper.valueToTree(command);
JsonCompat.setPropertyString(rval, Constants.PROP___TYPE, command.type());
return rval;
}
/**
* Unmarshal a command by reading from the provided JS object data.
* @param from
*/
public static ICommand unmarshallCommand(Object from) {
String type = JsonCompat.consumePropertyString(from, Constants.PROP___TYPE);
if (type == null) {
throw new RuntimeException("Missing __type from source data.");
}
try {
Class extends ICommand> cmdClass = CommandFactory.create(type).getClass();
ICommand cmd = mapper.treeToValue((TreeNode) from, cmdClass);
return cmd;
} catch (JsonProcessingException e) {
throw new RuntimeException("Error unmarshalling command: " + type, e);
} catch (NullPointerException e) {
throw new RuntimeException("Missing command from unmarshalling factory: " + type, e);
}
}
/**
* Marshalls the given simple type into a JS object.
* @param sType
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object marshallSimplifiedType(SimplifiedType sType) {
if (ModelUtils.isNullOrUndefined(sType)) {
return null;
}
Object obj = JsonCompat.objectNode();
JsonCompat.setPropertyString(obj, Constants.PROP_TYPE, sType.type);
// TODO convert from List