Java Primitive Type Create toPrimitiveWrapper(String primitiveJson, Class clazz)

Here you can find the source of toPrimitiveWrapper(String primitiveJson, Class clazz)

Description

to Primitive Wrapper

License

Apache License

Declaration

public static <T> T toPrimitiveWrapper(String primitiveJson, Class<T> clazz) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static <T> T toPrimitiveWrapper(String primitiveJson, Class<T> clazz) {
        Object result = null;//  ww w .  jav a 2s  .  c  om

        if (clazz.isAssignableFrom(Integer.class)) {
            result = Integer.valueOf(primitiveJson);

        } else if (clazz.isAssignableFrom(Long.class)) {
            result = Long.valueOf(primitiveJson);

        } else if (clazz.isAssignableFrom(Short.class)) {
            result = Short.valueOf(primitiveJson);

        } else if (clazz.isAssignableFrom(Float.class)) {
            result = Float.valueOf(primitiveJson);

        } else if (clazz.isAssignableFrom(Double.class)) {
            result = Double.valueOf(primitiveJson);

        } else if (clazz.isAssignableFrom(Byte.class)) {
            result = Byte.valueOf(primitiveJson);

        } else if (clazz.isAssignableFrom(Boolean.class)) {
            result = Boolean.valueOf(primitiveJson);

        } else if (clazz.isAssignableFrom(Character.class)) {
            result = Character.valueOf(primitiveJson.charAt(0));

        } else if (clazz.isAssignableFrom(Void.class)) {
            result = "";
        }

        return clazz.cast(result);
    }
}

Related

  1. toPrimitiveNumber(String s)
  2. toPrimitiveType(Class c)
  3. toPrimitiveType(Class cls)
  4. toPrimitiveType(Class type)
  5. toPrimitiveWrapper(Class type)