从NSData创建ENUM(Create ENUM from NSData)
有没有什么办法从NSData对象创建一个ENUM? 我从蓝牙设备收到NSData,让它更具可读性,我正在考虑使用ENUM。
我的代码现在:
let data = NSData(bytes: [0x00, 0x12] as [UInt8], length: 2) let initData = NSData(bytes: [0xa2,0x12] as [UInt8], length: 2) let openedData = NSData(bytes: [0xa1,0x12] as [UInt8], length: 2) let alreadyOpenedData = NSData(bytes: [0xa1,0x13] as [UInt8], length: 2) let finishedData = NSData(bytes: [0xa3,0x12] as [UInt8], length: 2) switch data{ case openedData: print("opened") break case alreadyOpenedData: print("alreadyOpened") break case initData: print("ini") break case finishedData: print("finished") default: print("error") }
Is there any way to create a ENUM from an NSData object ? I receive an NSData from a bluetooth device, to let it more readable i was thinking to use ENUM.
my code right now :
let data = NSData(bytes: [0x00, 0x12] as [UInt8], length: 2) let initData = NSData(bytes: [0xa2,0x12] as [UInt8], length: 2) let openedData = NSData(bytes: [0xa1,0x12] as [UInt8], length: 2) let alreadyOpenedData = NSData(bytes: [0xa1,0x13] as [UInt8], length: 2) let finishedData = NSData(bytes: [0xa3,0x12] as [UInt8], length: 2) switch data{ case openedData: print("opened") break case alreadyOpenedData: print("alreadyOpened") break case initData: print("ini") break case finishedData: print("finished") default: print("error") }
原文:https://stackoverflow.com/questions/35655157
最满意答案
我无法理解为什么这种方法不起作用。 我最终下载了jackson-datatype-joda模块。
此模块的唯一问题是它不允许自定义格式化日期时间,并且只能使用ISO 8601。
另一个问题是,在数据库中,日期时间被转换为纪元UTC时间,最后添加了4个零(为什么是零...)。 因此,当将其提取回客户端时,需要完成从纪元到可呈现日期的转换。
I couldn't understand why this method was not working. I ended up downloading the jackson-datatype-joda module.
The only issue with this module is that it yet does not allow custom formatters of date time and only operates with ISO 8601.
Another issue is that in the database the date time is converted to an epoch UTC time, with an added 4 zeros in the end (why the zeros..). So when fetching it back to the client a conversion from epoch to a presentable date needed to be done.
相关问答
更多-
我与杰克逊是一个完全的新手,但下面的作品适合我。 首先我给A添加一个JsonCreator方法: public class A { private String a1; private Integer a2; public String getA1() { return a1; } public Integer getA2() { return a2; } public void setA1(String a1) { this.a1 = a1; } publi ...
-
使用Jackson的自定义反序列化:扩展默认的反序列化器(Custom deserialization with Jackson: extend default deserializer)[2022-10-01]
正如沙龙所说(基于我如何称杰克逊的定制解串器的默认解串器 ) @Bean public ObjectMapper serializingObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.setDeserializerModifier(new BeanDeserializerModi ... -
最后找到了一个反序列化枚举的例子 ,尽管这两个例子中只有一个有效。 从他的枚举看,我看到他正在传递一个JsonNode 。 像魅力一样工作! @JsonCreator public static SomeEnum fromString(JsonNode json) { return SomeEnum.valueOf(json.get("code").asText()); } Finally found an example of deserializing an enum, though onl ...
-
我无法理解为什么这种方法不起作用。 我最终下载了jackson-datatype-joda模块。 此模块的唯一问题是它不允许自定义格式化日期时间,并且只能使用ISO 8601。 另一个问题是,在数据库中,日期时间被转换为纪元UTC时间,最后添加了4个零(为什么是零...)。 因此,当将其提取回客户端时,需要完成从纪元到可呈现日期的转换。 I couldn't understand why this method was not working. I ended up downloading the jack ...
-
问题解决了这样: class MyClass{ ... HttpStatus httpStatus = HttpStatus.OK //from org.springframework.http @JsonIgnore public HttpStatus getHttpStatus() { return httpStatus; } @JsonProperty(value = "HttpStatus") @JsonDeserializ ...
-
您正在寻找的方法是JsonDeserializationContext.deserialize() 。 根据有关如何导致无限循环的警告,这将调用您已设置的任何相关自定义反序列化器。 我相信用单行MySubItems subItems = context.deserialize(element, MySubItems.class);替换循环内的subItems的初始化MySubItems subItems = context.deserialize(element, MySubItems.class); 将完 ...
-
经过一些测试,我发现@jax的答案有问题。 正如@Staxman指出的那样,在构造反序列化器时会调用createContextual() ,而不是在每个反序列化过程中调用。 并且createContextual返回的反序列化器将由Jackson库缓存。 因此,如果您的反序列化器使用多于一种类型(例如公共父类型的子类型),它将抛出类型不匹配异常,因为targetClass属性将是Jackson库缓存的最后一种类型。 正确的解决方案应该是: public class JsonApiDeserializer ex ...
-
如何在自定义反序列化器中使用一些Jackson Deserializer?(How to use some Jackson Deserializer in own custom Deserializer?)[2022-05-07]
如果您的输入表示ZonedDateTime并且您想将其转换为LocalDateTime ,则可以执行以下操作。 我创建了一个带有LocalDateTime字段的示例类: public class ZoneToLocalTest { @JsonDeserialize(using = CustomZonedToLocalDeserializer.class) private LocalDateTime date; // getter and setter } 并且还创建了反序列化器 ... -
Spring JSON Serializer和Deserializer没有被调用(Spring JSON Serializer and Deserializer is not getting called)[2022-07-19]
你真的混合了一些东西;)MongoDB确实以类似JSON的格式(BSON - Binary JSON)存储数据,但它与Jackson没有任何关系 - 它被用来(就像你展示的那样)序列化/反序列化通过HTTP发送的对象。 在Spring Data MongoDB中,您需要实现和配置自定义转换器。 有关详细信息,请参阅此处 。 You really mixed some things ;) MongoDB indeed stores data in JSON-like format (BSON - Binary ... -
JsonSerializer和JsonDeserializer紧密绑定到Gson JSON树模型和特定的Gson配置(de)序列化上下文,它提供了一组可以(反)序列化的类型。 因此,完成JsonSerializer和JsonDeserializer单元测试相当困难。 在src/test/resources/.../zoned-date-time.json某处考虑以下JSON文档: "Mon Mar 27 14:09:47 +0000 2017" 这是一个非常有效的JSON文档,除了简单的单个字符串外,它没 ...