首页 \ 问答 \ 什么时候JsonNode实例为null?(When is a JsonNode instance null?)

什么时候JsonNode实例为null?(When is a JsonNode instance null?)

考虑下面的Java类定义。

CrudOperation.java:

package com.cyberfront.test.json.nll.demonstration;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JsonNode;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CrudOperation{
    public enum Type {CREATE,UPDATE,DELETE,READ}
    private final Type type;
    private final JsonNode op;

    public CrudOperation(Type type) { this(type, null); }
    public CrudOperation(JsonNode op) { this(Type.UPDATE, op); }

    @JsonCreator
    public CrudOperation(@JsonProperty("type") Type type, @JsonProperty("op") JsonNode op) {
        this.type = type;
        this.op = op;

        boolean isUpdate = Type.UPDATE.equals(this.getType());
        boolean isNotNull = null == this.getOp();
        boolean isValid = isUpdate ^ isNotNull;

        if (!isValid) {
            System.out.println(" isUpdate: " + String.valueOf(isUpdate));
            System.out.println("isNotNull: " + String.valueOf(isNotNull));
            System.out.println("  isValid: " + String.valueOf(isValid));

            throw new IllegalArgumentException("Operation Failed Validation: " + this.toString());
        }
    }

    @JsonProperty("type")
    public Type getType() { return this.type; };

    @JsonProperty("op")
    public JsonNode getOp() { return this.op; }

    public static <T> String nullCheck(T val) { return null == val ? "null" : val.toString(); }

    public static String toString(Type type, JsonNode op) {
        return "{\"type\":\"" + nullCheck(type) + "\",\"op\":" + nullCheck(op) + "}";
    }

    @Override
    public String toString() { return toString(this.getType(), this.getOp()); }
}

在这个类中,带有@JsonCreator注解的构造函数执行验证,以确保当type参数的值为Type.UPDATE时确实存在非空JsonNode参数。 也就是说,当type值为Type.UPDATEop应该是非空的。 如果type有任何其他值,则op应该为空。

接下来考虑下面的类。

Main.java:

package com.cyberfront.test.json.nll.demonstration;

import com.cyberfront.test.json.nll.demonstration.CrudOperation.Type;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

    private static ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) {
        CrudOperation createObject = new CrudOperation(Type.CREATE);
        System.out.println("Initial value: " + createObject.toString());

        JsonNode createDocument = mapper.valueToTree(createObject);
        System.out.println("Document value: " + createDocument.toString());

        CrudOperation reconstructedObject = null;
        try {
            reconstructedObject = mapper.treeToValue(createDocument, CrudOperation.class);
        } catch (JsonProcessingException e) {
            System.out.println(e);
        }

        System.out.println("Reconstructed: " + CrudOperation.nullCheck(reconstructedObject));
    }
}

这用于测试操作类型值为Type.CREATECrudOperation实例的创建,这意味着op应该为null。 但是,这会产生以下输出:

Initial value: {"type":"CREATE","op":null}
Document value: {"@type":"CrudOperation","type":"CREATE"}
 isUpdate: false
isNotNull: false
  isValid: false
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.cyberfront.test.json.nll.demonstration.CrudOperation, problem: Operation Failed Validation: {"type":"CREATE","op":null}
 at [Source: N/A; line: -1, column: -1]
Reconstructed: null

最初的CrudOperation实例化已经过验证,并且该实例转换为JsonNode实例也可以正常工作。 尝试从原始派生的JsonNode重构CrudOperation实例时,会发生此问题。 出于某种原因, op的值显示为null ,但与null进行相等性测试失败,这反过来又导致CrudOperation验证失败。

为什么会发生,怎么解决?


Consider the following Java class definition.

CrudOperation.java:

package com.cyberfront.test.json.nll.demonstration;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JsonNode;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CrudOperation{
    public enum Type {CREATE,UPDATE,DELETE,READ}
    private final Type type;
    private final JsonNode op;

    public CrudOperation(Type type) { this(type, null); }
    public CrudOperation(JsonNode op) { this(Type.UPDATE, op); }

    @JsonCreator
    public CrudOperation(@JsonProperty("type") Type type, @JsonProperty("op") JsonNode op) {
        this.type = type;
        this.op = op;

        boolean isUpdate = Type.UPDATE.equals(this.getType());
        boolean isNotNull = null == this.getOp();
        boolean isValid = isUpdate ^ isNotNull;

        if (!isValid) {
            System.out.println(" isUpdate: " + String.valueOf(isUpdate));
            System.out.println("isNotNull: " + String.valueOf(isNotNull));
            System.out.println("  isValid: " + String.valueOf(isValid));

            throw new IllegalArgumentException("Operation Failed Validation: " + this.toString());
        }
    }

    @JsonProperty("type")
    public Type getType() { return this.type; };

    @JsonProperty("op")
    public JsonNode getOp() { return this.op; }

    public static <T> String nullCheck(T val) { return null == val ? "null" : val.toString(); }

    public static String toString(Type type, JsonNode op) {
        return "{\"type\":\"" + nullCheck(type) + "\",\"op\":" + nullCheck(op) + "}";
    }

    @Override
    public String toString() { return toString(this.getType(), this.getOp()); }
}

In this class, the constructor with the @JsonCreator annotation performs validation to ensure there is a non-null JsonNode argument op exactly when the type argument has the value Type.UPDATE. That is, when type has value Type.UPDATE, op should be non-null. If type has any other value, op should be null.

Next consider the class below.

Main.java:

package com.cyberfront.test.json.nll.demonstration;

import com.cyberfront.test.json.nll.demonstration.CrudOperation.Type;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

    private static ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) {
        CrudOperation createObject = new CrudOperation(Type.CREATE);
        System.out.println("Initial value: " + createObject.toString());

        JsonNode createDocument = mapper.valueToTree(createObject);
        System.out.println("Document value: " + createDocument.toString());

        CrudOperation reconstructedObject = null;
        try {
            reconstructedObject = mapper.treeToValue(createDocument, CrudOperation.class);
        } catch (JsonProcessingException e) {
            System.out.println(e);
        }

        System.out.println("Reconstructed: " + CrudOperation.nullCheck(reconstructedObject));
    }
}

This is used to test the creation of a CrudOperation instance with operation type value of Type.CREATE, which means op should be null. Yet, this produces the following output:

Initial value: {"type":"CREATE","op":null}
Document value: {"@type":"CrudOperation","type":"CREATE"}
 isUpdate: false
isNotNull: false
  isValid: false
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.cyberfront.test.json.nll.demonstration.CrudOperation, problem: Operation Failed Validation: {"type":"CREATE","op":null}
 at [Source: N/A; line: -1, column: -1]
Reconstructed: null

The initial CrudOperation instantiation is validated, and the conversion of that instance to a JsonNode instance also works properly. The problem occurs when attempting to reconstitute the CrudOperation instance from the JsonNode derived from the original. For some reason, the value of op displays as null, but the test for equality with null fails, which in turn causes the CrudOperation validation to fail.

Why is happening and how can it be resolved?


原文:https://stackoverflow.com/questions/47483118
更新时间:2021-08-22 15:08

最满意答案

vec3 lightDir = vec3(1,1,0)是常数,而每次相机改变时都会转换normal

为防止这种情况,请确保lightDirnormal位于同一空间。 在变换normal之前计算dot ,或者通过gl_NormalMatrix变换gl_NormalMatrix然后计算dot

除了您想要实现的效果之外,片段着色器中还存在问题: normal未标准化。 这是因为单位矢量的线性插值不会总是产生单位矢量。

你应该做的是:

color*dot(normalize(normal),  transform_to_same_space(lightDir))*inten;

第二部分中的一些小问题:

你是在没有类型的情况下声明modelMatrix

你不能用4x4矩阵转换vec3 。 您可以使用0填充gl_Normal ,因为它是向量或将矩阵转换为float3x3

你正在使用带有Vector3f(1,1,1) Matrix4f.scale ,并且平移不会影响向量,因此modelMatrix是正交的。 但是,在一般情况下,您应该使用modelMatrix的转置反转换gl_Normal


vec3 lightDir = vec3(1,1,0) is constant while normal is transformed every time the camera changes.

To prevent this make sure lightDir and normal are in the same space. Compute the dot before transforming normal or, transform lightDir by gl_NormalMatrix then compute the dot.

In addition to the effect you want to achieve you have an issue in the fragment shader: normal is not normalized. This is because a linear interpolation of unit vectors won't always produce a unit vector.

What you should do is something like:

color*dot(normalize(normal),  transform_to_same_space(lightDir))*inten;

Some minor issues in the second part:

You're declaring modelMatrix without a type.

You cannot transform vec3 with a 4x4 matrix. You can pad gl_Normal with a 0 since it's a vector or cast down the matrix to float3x3.

You're using Matrix4f.scale with Vector3f(1,1,1) and the translation won't affect vectors so modelMatrix is orthonormal. However, in the general case you should transform gl_Normal with the transposed inverse of modelMatrix.

相关问答

更多
  • OpenGL wiki提供了一个很好的定义: Shader是一个用户定义的程序,设计用于在图形处理器的某个阶段运行。 过去,图形卡是不可编程的硅片,它执行一组固定的算法:点/颜色/光线进入,并且使用固定算法(通常沿着https://en.wikipedia)出现2D图像。 org / wiki / Phong_reflection_model )。 但对于想要创建许多不同视觉效果的程序员而言,这太过于严格。 随着技术的进步,GPU供应商开始允许渲染管线的一些部分被编程为编程语言,如GLSL。 然后,这些语言 ...
  • 在Xcode中使用SpriteKit场景编辑器。 当您将着色器源文件与检查器中的节点相关联时,场景视图会立即为您提供该着色器的实时预览。 然后,您可以使用Xcode的助理编辑器窗格来查看和编辑着色器的源与场景中当前选定的节点相关联 - 完成着色器中任何动画的实时预览(来自u_time输入)。 这会让你突出语法,但不会自动完成。 Xcode的完成引擎似乎仅限于其主要的编译器工具链 - 将它支持通常的着色器当然是一个很好的事情来提出一个错误 。 编辑着色器源应该更新实时预览,但似乎至少有一些Xcode版本,您必 ...
  • 你可以通过一个NaN的条件来检查NaN: bool isNan(float val) { return (val <= 0.0 || 0.0 <= val) ? false : true; } isinf有点困难。 没有机制将float转换为整数表示并使用位。 所以你必须将它与一个适当的大数进行比较。 You can check for NaN via a condition that will only be true for NaN: bool isNan(float val) { retur ...
  • 您是否将着色器选择模式与着色器一起使用? 如果是这样,这可能是您的问题:选择模式可能会触发回退到软件实现; 我知道在GPU上没有选择模式的OpenGL实现。 由于这个代码路径很少被使用,因此它很可能是错误的。 特别是如果与着色器一起使用,必须在GPU上进行仿真。 如果着色器JIT编译器发出错误代码,您将会崩溃。 最重要的是,选择模式已从全新的OpenGL版本中删除。 您应该使用单通道整数颜色缓冲附件渲染到Framebuffer对象,而不是选择模式(容易出错且速度慢)。 每个对象都以不同的“索引”颜色呈现。 ...
  • 如果是静态显示相同图像的情况,那么使用纹理或显示列表就足够了。 但是,如果你想经常更新它,着色器确实是最好的选择。 Shader代码在GPU上执行并修改GRAM中的数据,因此您不需要从CPU转移到GPU。 接下来最好的事情可能是一个像素或帧缓冲区对象。 缓冲区对象允许您通过DMA读取/写入GRAM(无需通过CPU),因此它们可以非常快速。 我还没有写任何着色器,所以我不推荐任何好的资源。 但是SongHo的OpenGL页面是学习缓冲区对象的好地方。 (尽管他的例子是在C ++中) If it were th ...
  • uMVPMatrix应该是u_MVMatrix 编译着色器时尝试获取编译日志。 uMVPMatrix should be u_MVMatrix Try to get a compile log when compiling shaders.
  • GPU与CPU是一个讨论的主题,正如@Nicol Bolas所指出的那样,问题是寻找一个通用解决方案,其中多种因素会影响结果。 在考虑性能时,没有什么比分析更好。 众所周知,人类在预测其应用程序的性能影响方面表现不佳。 如果在开发过程中你注意到你突然受到GPU限制,那么你可能想要将一些工作卸载到CPU。 另一方面,如果您受CPU限制,您可以选择将更多工作卸载到GPU,即使该工作与图形无关。 很可能你会想要通过为GPU提供尽可能多的东西(特别是如果它是图形相关的)来释放你的CPU周期。 您可以阅读更多关于在N ...
  • 这两个着色器程序是否会有效地缓存,这应该不是问题? 问题不在于缓存,而是更改着色器会刷新GPU执行管道。 管道和分支预测器需要几十个时钟周期才能满足新切换到着色器的要求。 我应该组合两个着色器并使用glDrawBuffers()指示输出? 在延迟着色器设置中,如果你可以做到没有太多问题? 非也! Will the two shader programs be cached effectively enough that this shouldn't be a problem? The problem is ...
  • 这是必要的,因为GPU架构可能有很大差异吗? 确实如此,虽然已经提到了着色器的IR版本,但对它的外观并没有达成一致。 您可以使用glShaderBinary在先前的运行中预编译着色器并重用已编译的输出。 Is this necessary because GPU architectures can vary greatly? Yes indeed and while there has been mentioning of an IR version of shaders there hasn't been ...
  • vec3 lightDir = vec3(1,1,0)是常数,而每次相机改变时都会转换normal 。 为防止这种情况,请确保lightDir和normal位于同一空间。 在变换normal之前计算dot ,或者通过gl_NormalMatrix变换gl_NormalMatrix然后计算dot 。 除了您想要实现的效果之外,片段着色器中还存在问题: normal未标准化。 这是因为单位矢量的线性插值不会总是产生单位矢量。 你应该做的是: color*dot(normalize(normal), trans ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。