在不使用ad-hoc解析器实现的情况下反序列化许多网络消息(Deserializing many network messages without using an ad-hoc parser implementation)
我有一个关于反序列化的问题。 我可以设想使用Data.Data,Data.Typeable或GHC.Generics的解决方案,但我很好奇它是否可以在没有泛型,SYB或元编程的情况下完成。
问题描述:
给定已知包含本地定义的代数数据类型字段的[String]列表,我想反序列化[String]以构造目标数据类型。 我可以编写一个解析器来执行此操作,但我正在寻找一种通用解决方案,它将反序列化为程序中定义的任意数量的数据类型,而无需为每种类型编写解析器。 通过知道代数类型的值构造函数的数量和类型,就像对每个字符串执行读取以生成构建类型所需的适当值一样简单。 但是,我不想使用泛型,反射,SYB或元编程(除非它不可能)。
假设我有大约50种与此类似的定义(所有简单的代数类型由基本原语组成(没有嵌套或递归类型,只是基元的不同组合和排序):
data NetworkMsg = NetworkMsg { field1 :: Int, field2 :: Int, field3 :: Double} data NetworkMsg2 = NetworkMsg2 { field1 :: Double, field2 :: Int, field3 :: Double }
我可以使用我在每个[String]之前解析的标记ID来确定与我通过网络收到的[String]相关联的数据类型。
可能的推测解决方案路径:
由于数据构造函数是Haskell中的第一类值,并且实际上具有类型 - 可以将NetworkMsg构造函数视为函数,例如:
NetworkMsg :: Int -> Int -> Double -> NetworkMsg
我可以使用uncurryN将此函数转换为元组的函数,然后将[String]复制到函数现在采用的相同形状的元组中吗?
NetworkMsg' :: (Int, Int, Double) -> NetworkMsg
我认为这不会起作用,因为我需要知道值构造函数和类型信息,这需要Data.Typeable,反射或其他一些元编程技术。
基本上,我正在寻找许多类型的自动反序列化,而无需编写类型实例声明或在运行时分析类型的形状。 如果不可行,我会采用另一种方式。
I have a question pertaining to deserialization. I can envision a solution using Data.Data, Data.Typeable, or with GHC.Generics, but I'm curious if it can be accomplished without generics, SYB, or meta-programming.
Problem Description:
Given a list of [String] that is known to contain the fields of a locally defined algebraic data type, I would like to deserialize the [String] to construct the target data type. I could write a parser to do this, but I'm looking for a generalized solution that will deserialize to an arbitrary number of data types defined within the program without writing a parser for each type. With knowledge of the number and type of value constructors an algebraic type has, it's as simple as performing a read on each string to yield the appropriate values necessary to build up the type. However, I don't want to use generics, reflection, SYB, or meta-programming (unless it's otherwise impossible).
Say I have around 50 types defined similar to this (all simple algebraic types composed of basic primitives (no nested or recursive types, just different combinations and orderings of primitives) :
data NetworkMsg = NetworkMsg { field1 :: Int, field2 :: Int, field3 :: Double} data NetworkMsg2 = NetworkMsg2 { field1 :: Double, field2 :: Int, field3 :: Double }
I can determine the data-type to be associated with a [String] I've received over the network using a tag id that I parse before each [String].
Possible conjectured solution path:
Since data constructors are first-class values in Haskell, and actually have a type-- Can NetworkMsg constructor be thought of as a function, such as:
NetworkMsg :: Int -> Int -> Double -> NetworkMsg
Could I transform this function into a function on tuples using uncurryN then copy the [String] into a tuple of the same shape the function now takes?
NetworkMsg' :: (Int, Int, Double) -> NetworkMsg
I don't think this would work because I'd need knowledge of the value constructors and type information, which would require Data.Typeable, reflection, or some other metaprogramming technique.
Basically, I'm looking for automatic deserialization of many types without writing type instance declarations or analyzing the type's shape at run-time. If it's not feasible, I'll do it an alternative way.
原文:https://stackoverflow.com/questions/30549890
最满意答案
Android使用不同的类文件格式。 您是否通过Android SDK附带的“dx”工具运行第三方JAR文件?
Android uses a different class file format. Are you running the 3rd party JAR files through the "dx" tool that ships with the Android SDK?
相关问答
更多-
Android使用不同的类文件格式。 您是否通过Android SDK附带的“dx”工具运行第三方JAR文件? Android uses a different class file format. Are you running the 3rd party JAR files through the "dx" tool that ships with the Android SDK?
-
1.6中的startactivity上的android java.lang.VerifyError(android java.lang.VerifyError on startactivity in 1.6)[2022-10-20]
您正在使用1.6以上版本的SDK中的函数,但是您在清单中声称您的应用在1.6上运行良好。 将您的构建目标设置为1.6以查看哪些目标。 You're using functions from an SDK past 1.6, but you claim in your manifest that your app runs fine on 1.6. Set your build target to 1.6 to see which ones. -
我得到了同样的错误。我认为这是hugo导致的问题。 不要使用@ DebugLog,我运行我的应用程序没有问题 I get the same error.I think it is hugo lead to the problem . Don't use @DebugLog,I run my app without the problem
-
Android Studio 0.5.4更新导致Java.Lang.VerifyError(Android Studio 0.5.4 update causes Java.Lang.VerifyError)[2022-05-26]
当您针对与运行时使用的库不同的库进行编译时,会导致java.lang.VerifyError 。 在运行时,当方法的签名不匹配时,将引发错误。 或者当有编译器错误时(尽管很少见)。 检查这一行: Could not find method com.google.android.gms.common.api.GoogleApiClient.isConnected, referenced........ 您似乎没有使用在编译时使用的相同库。 java.lang.VerifyError is caused wh ... -
我是一个很棒的Android粉丝,但有时它是不可原谅的:( 错误中有一条线索表明该错误涉及静态初始化器; 这是
“方法”中的名称: WARN/dalvikvm(2114): VFY: rejected Lcom/examplecompany/project/TidalStreams/Data/gTidalStreamsHourlyHW;. ()V 这表明错误可能与static { ... }初始化程序块或某些static类成员的初始化有关。 我的类有一个静态数组初始化器: ... -
我通过减少使用此方法的次数来解决此问题: SharedPreferences preferencias = getSharedPreferences("CATS", 0); String sid = preferencias.getString("sid", null); 我在其中一个类中使用了静态字段,它工作得很好。 I solved this problem by reducing the number of times when I use this method: SharedPreference ...
-
有任何想法吗? SessionManager使用的是Android中不存在的类或方法。 假设这是相关课程的来源,没有任何方法可以在没有实质性修改的情况下在Android上运行,因为它使用: Apache HttpClient的旧版本 JDOM 两者都不在Android中。 Any ideas? SessionManager is using a class or method that does not exist in Android. Assuming this is the source for th ...
-
Intent.EXTRA_DOCK_STATE仅存在于API级别5及更高版本中,因此仅适用于Android 2.0设备(或更高版本)。 即使您在API级别检查中包装调用,在Android 1.6运行时运行代码时代码也会失败,因此验证VerifyError 。 解决方案是将Intent.EXTRA_DOCK_STATE的调用替换为其常量值: android.intent.extra.DOCK_STATE 。 作为一般规则,在浏览API文档时勾选“按API级别过滤”复选框是个好主意,并在您的情况下将其设置为4。 ...
-
某处,也许在onPerformSync() ,您的代码引用了OperationCanceledException 。 该类仅存在于API级别16+上,您似乎试图在较旧的Android设备上使用它。 Somewhere, perhaps in onPerformSync(), your code is referencing OperationCanceledException. That class only exists on API Level 16+, and you would appear to ...
-
可能重复的Android java.lang.VerifyError? 在VerifyError消息之上是否有任何输出,如此答案提到的那样? https://stackoverflow.com/a/3549131 Possible duplicate of Android java.lang.VerifyError? Is there any output above the VerifyError message, like this answer mentions? https://stackoverf ...