All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsonex.jsoncoder.coder.CoderClass Maven / Gradle / Ivy

There is a newer version: 0.1.27
Show newest version
/*************************************************************
 Copyright 2018-2019 eBay Inc.
 Author/Developer: Jianwu Chen

 Use of this source code is governed by an MIT-style
 license that can be found in the LICENSE file or at
 https://opensource.org/licenses/MIT.
 ************************************************************/

package org.jsonex.jsoncoder.coder;

import org.jsonex.core.factory.InjectableInstance;
import org.jsonex.jsoncoder.BeanCoderContext;
import org.jsonex.jsoncoder.BeanCoderException;
import org.jsonex.jsoncoder.ICoder;
import org.jsonex.treedoc.TDNode;

import java.lang.reflect.Type;

@SuppressWarnings("rawtypes")
public class CoderClass implements ICoder {
  public static final InjectableInstance it = InjectableInstance.of(CoderClass.class);
  public static CoderClass get() { return it.get(); }

  @Override public Class getType() { return Class.class; }

  @Override public TDNode encode(Class obj, Type type, BeanCoderContext context, TDNode target) {
    return target.setValue(obj.getCanonicalName());
  }

  @Override public Class decode(TDNode jsonNode, Type type, Object targetObj, BeanCoderContext context) {
    try {
      return Class.forName((String) jsonNode.getValue());
    } catch (Exception e) {
      throw new BeanCoderException("Can't load class: " + jsonNode.getValue(), e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy