首页 \ 问答 \ 在不使用ad-hoc解析器实现的情况下反序列化许多网络消息(Deserializing many network messages without using an ad-hoc parser implementation)

在不使用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
更新时间:2023-08-16 12:08

最满意答案

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以上版本的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
  • 当您针对与运行时使用的库不同的库进行编译时,会导致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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)