Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSONB1.0/EclipseYasson忽略没有bean访问器方法的私有属性_Java_Json_Maven_Jsonb Api_Yasson - Fatal编程技术网

Java JSONB1.0/EclipseYasson忽略没有bean访问器方法的私有属性

Java JSONB1.0/EclipseYasson忽略没有bean访问器方法的私有属性,java,json,maven,jsonb-api,yasson,Java,Json,Maven,Jsonb Api,Yasson,根据我假设的是官方用户指南,引擎应该序列化它找到的任何属性,不管有没有bean访问器方法(我知道Dog示例使用公共字段,但私有字段请参见Person示例) 鉴于这些类别: public class Rectangle { private double length1 = 0.0; @JsonbProperty("length2") private double length2 = 0.0; public double width = 0.0; } public

根据我假设的是官方用户指南,引擎应该序列化它找到的任何属性,不管有没有bean访问器方法(我知道Dog示例使用公共字段,但私有字段请参见Person示例)

鉴于这些类别:

public class Rectangle {
    private double length1 = 0.0;
    @JsonbProperty("length2")
    private double length2 = 0.0;
    public double width = 0.0;
}

public class Rectangle2 {
    @JsonbProperty
    private double length = 0.0;

    private double width = 0.0;

    public double getLength() {
        return length;
    }

    public double getWidth() {
        return width;
    }
}
当我像这样序列化它时:

public class App2 {
    public static void main(String... argv) {
        Jsonb jsonb = JsonbBuilder.create();

        System.out.println("Rectangle: " + jsonb.toJson(new Rectangle()));
        System.out.println("Rectangle2: " + jsonb.toJson(new Rectangle2()));
    }
}
    <dependency>
        <groupId>javax.json.bind</groupId>
        <artifactId>javax.json.bind-api</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse</groupId>
        <artifactId>yasson</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.1.4</version>
    </dependency>
输出如下:

Rectangle: {"width":0.0}
Rectangle2: {"length":0.0,"width":0.0}
我看到的是,在矩形中,只有宽度是序列化的,因为它是公共的。length1和length2被忽略,因为它们是私有的,即使length2上有属性注释。Rectangle2是完全序列化的,因为它有bean方法

一定要这样吗?要求我将所有字段公开并可变以启用序列化似乎是一个巨大的限制

我的依赖项设置如下:

public class App2 {
    public static void main(String... argv) {
        Jsonb jsonb = JsonbBuilder.create();

        System.out.println("Rectangle: " + jsonb.toJson(new Rectangle()));
        System.out.println("Rectangle2: " + jsonb.toJson(new Rectangle2()));
    }
}
    <dependency>
        <groupId>javax.json.bind</groupId>
        <artifactId>javax.json.bind-api</artifactId>
        <version>1.0</version>
    </dependency>

    <dependency>
        <groupId>org.eclipse</groupId>
        <artifactId>yasson</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.1.4</version>
    </dependency>

javax.json.bind
javax.json.bind-api
1
org.eclipse
亚森
1.0.2
玻璃鱼
javax.json
1.1.4

我在yasson源代码(org.eclipse.yasson.internal.model.PropertyValuePropagation.DefaultVisibilityStrategy)中找到了关于规范和字段可见性的参考:

我不能谈论规范,但这与我所看到的不一样——字段将仅基于getter方法的可见性进行序列化

我希望我的序列化只由字段驱动,并且只由我希望序列化的字段驱动——因此我采用了一种定制的PropertyVisibilityStrategy,它不公开任何方法,只公开带有JsonbProperty注释的字段。这让我得到了我想要的大部分:

    Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(
        new JsonbConfig().withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
            @Override
            public boolean isVisible(Field field) {
                return field.getAnnotation(JsonbProperty.class) != null;
            }

            @Override
            public boolean isVisible(Method method) {
                return false;
            }
        })
    ).build();

我在yasson源代码(org.eclipse.yasson.internal.model.PropertyValuePropagation.DefaultVisibilityStrategy)中找到了关于规范和字段可见性的参考:

我不能谈论规范,但这与我所看到的不一样——字段将仅基于getter方法的可见性进行序列化

我希望我的序列化只由字段驱动,并且只由我希望序列化的字段驱动——因此我采用了一种定制的PropertyVisibilityStrategy,它不公开任何方法,只公开带有JsonbProperty注释的字段。这让我得到了我想要的大部分:

    Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(
        new JsonbConfig().withPropertyVisibilityStrategy(new PropertyVisibilityStrategy() {
            @Override
            public boolean isVisible(Field field) {
                return field.getAnnotation(JsonbProperty.class) != null;
            }

            @Override
            public boolean isVisible(Method method) {
                return false;
            }
        })
    ).build();