简体   繁体   English

在Java数组中存储任意原始类型

[英]Storing arbitrary primitive types in an a java array

I have a use case where I must parse JSON into primitive values in Java. 我有一个用例,其中必须将JSON解析为Java中的原始值。 I have instructive fields embedded in the JSON that informs my parsing which primitive to deserialize to. 我在JSON中嵌入了说明性字段,该字段通知我解析要反序列化到的原语。 These primitive types must be added to an array of some length. 这些原始类型必须添加到一定长度的数组中。

So I may have some JSON like this: 所以我可能有一些像这样的JSON:

"primitives" : [
{
"valueType" : "int",
"value" : 3
},
{
"valueType" : "double",
"value" : 4
},
]

I have written the code to properly parse this JSON into two primitives, one int and double with values 3 and 4. However, because I am adding them to an ArrayList which expects Objects they are autoboxed into Java's Integer and Double types. 我已经编写了将这个JSON正确解析为两个基元的代码,一个是int,另一个是具有值3和4的double。但是,因为我将它们添加到ArrayList中,该对象期望将对象自动装箱为Java的Integer和Double类型。 I thought of using a regular Java Array but there is still the problem of specifying the element type like Object[] arr where I have the same problem, or int[] where I am being too specific. 我曾考虑过使用常规的Java数组,但是仍然存在在我遇到相同问题时指定诸如Object[] arrint[]过于具体的元素类型的问题。

Is there some functionality within Java that can allow me to parse this JSON to the correct Array of primitives. Java中是否有一些功能可以使我将此JSON解析为正确的原语数组。

One solution I have considered is an object that has all the different primitives as properties but this seems like too much complexity if a language level path is available. 我考虑过的一种解决方案是将所有不同的原语作为属性的对象,但是如果可以使用语言级别的路径,这似乎太复杂了。

Assuming that it is important to keep the original ordering (thus a single array), and that keeping track of the type is important, and that using a JSON parser is unavailable, I would consider something like the following. 假设保持原始顺序很重要(因此是一个数组),并且跟踪类型很重要,并且使用JSON解析器不可用,那么我将考虑以下内容。

enum ValueType { INT, DOUBLE, FLOAT };

static abstract class ParsedValue<T>
{
    private final T data;
    private final ValueType type;

    public ParsedValue(T val, ValueType t)
    {
        data = val;
        type = t;
    }

    public ValueType getType()
    {
        return type;
    }

    public T getValue()
    {
        return data;
    }
}

static class IntParsedValue extends ParsedValue<Integer>
{
    public IntParsedValue(Integer val)
    {
        super(val, ValueType.INT);
    }
}

static class DoubleParsedValue extends ParsedValue<Double>
{
    public DoubleParsedValue(Double val)
    {
        super(val, ValueType.DOUBLE);
    }
}


public static void main(String[] args)
{
    List<ParsedValue<?>> lst = new ArrayList<>();

    Random rnd = new Random();

    for (int i = 0; i < 25; ++i) {
        ParsedValue<?> pv;            
        if (rnd.nextInt(2) == 0) {
            pv = new IntParsedValue(rnd.nextInt(500));
        }
        else {
            pv = new DoubleParsedValue(rnd.nextDouble());
        }

        lst.add(pv);
    }

    for (ParsedValue<?> pv : lst) {
        switch (pv.getType()) {
        case INT:
            System.out.println("Integer: " + pv.getValue());
            break;

        case DOUBLE:
            System.out.println("Double: " + pv.getValue());
            break;

        case FLOAT:
            //...
            break;
        }


    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM