type-conversion 相关问题

类型转换是隐式或显式地将一种数据类型的实体更改为另一种数据类型的方式。这样做是为了利用类型层次结构或类型表示的某些功能。

无需反序列化的Google Protobuf

通常,google protobuf 或nanopb 实现旨在分别在服务器/客户端上序列化/反序列化消息。 然而,我面临着序列化的情况

回答 1 投票 0

如何加载具有自定义类型和自定义类型列表的字典

我正在开发一个统一项目,您可以在其中保存具有自定义类型 GraphNode 的键的字典,并且每个键的值是相同类型 GraphNode Dictionary 的列表 我正在开发一个统一项目,您可以在其中保存具有自定义类型 GraphNode 的键的字典,并且每个键的值都是相同类型 GraphNode 的列表Dictionary<GraphNode, List<GraphNode>>。我正在使用 NewtonSoft 库将 json 从对象转换为 json 文本以及从文本转换为对象。 游戏数据脚本: using System; using System.Collections.Generic; [Serializable] public class GameData { public Dictionary<GraphNode, List<GraphNode>> allGraphNodes; public GameData() { allGraphNodes = new Dictionary<GraphNode, List<GraphNode>>(); } } GraphNode 类: public class GraphNode { public enum Type { entrance = 0, enemy = 1, hub = 2, shop = 3, boss = 4 } public Type type; public GraphNode(Type type2) { SetType(type2); } public void SetType(Type type2) { type = type2; } } 我在stackoverflow上尝试了类似的例子,但它不起作用,这是为了保存: public void Save(GameData data) { string text = Path.Combine(dataDirPath, dataFileName); try { Directory.CreateDirectory(Path.GetDirectoryName(text)); string text3 = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); if (useEncryption) { text3 = EncryptDecrypt(text3); } using FileStream stream = new FileStream(text, FileMode.Create); using StreamWriter streamWriter = new StreamWriter(stream); streamWriter.Write(text3); } catch (Exception ex) { Debug.LogError("Error" + text + "\n" + ex); } } 保存的字典示例如下所示: { "$type": "GameData, Assembly-CSharp", "allGraphNodes": { "$type": "System.Collections.Generic.Dictionary`2[[GraphNode, Assembly-CSharp],[System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib]], mscorlib", "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 0 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 3 }, { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 }, { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 } ] } } } 这用于加载(出现错误的部分被标记为重要行): public GameData LoadLayout() { string text = Path.Combine(dataDirPath, dataFileName); GameData result = null; if (File.Exists(text)) { try { string text2 = ""; using (FileStream stream = new FileStream(text, FileMode.Open)) { using StreamReader streamReader = new StreamReader(stream); text2 = streamReader.ReadToEnd(); Debug.Log(text2); } if (useEncryption) { text2 = EncryptDecrypt(text2); } result = JsonConvert.DeserializeObject<GameData>(text2, new JsonSerializerSettings //important line { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); return result; } catch (Exception ex) { Debug.LogError("Error" + text + "\n" + ex); return result; } } return result; } 错误打印: Newtonsoft.Json.JsonSerializationException:无法将字符串“GraphNode”转换为字典键类型“GraphNode”。创建一个 TypeConverter 将字符串转换为键类型对象。路径“allGraphNodes.GraphNode”,第 5 行,位置 16。 ---> Newtonsoft.Json.JsonSerializationException:将值“GraphNode”转换为类型“GraphNode”时出错。路径“allGraphNodes.GraphNode”,第 5 行,位置 16。 ---> System.ArgumentException:无法从 System.String 转换或转换为 GraphNode。 您遇到几个问题: 您的字典具有复杂的键,但是,正如文档中所述,由于 Json.NET 默认将字典序列化为 JSON 对象,因此序列化仅适用于可转换为字符串的键。 因此,您需要将 allGraphNodes 序列化为数组。 将字典序列化为(键值对)数组中显示的方法有多种,但最简单的可能是使用代理数组属性序列化字典。 字典中的 List<GraphNode> 值可能引用字典中的其他键。要在序列化过程中保留这些引用,您需要在启用引用保留的情况下进行序列化。 不幸的是,引用保存不适用于参数化构造函数,因此GraphNode将需要一个无参数构造函数。只要标上[JsonConstructor]就可以私密。 您正在使用TypeNameHandling.All进行序列化,但是正如文档中所述,这会带来安全风险。有关详细信息,请参阅 Newtonsoft Json 中的 TypeNameHandling 警告 和 由于 Json.Net TypeNameHandling auto?,外部 json 容易受到攻击?它还可能导致 JSON 文件出现不必要的膨胀。 您当前的数据模型不需要此设置,因为它不使用多态性。如果您确实认为有必要,请考虑编写自定义的序列化绑定器。 将所有这些放在一起,如果您按如下方式修改数据模型: [Serializable] public class GameData { [JsonIgnore] public Dictionary<GraphNode, List<GraphNode>> allGraphNodes; public GameData() { allGraphNodes = new Dictionary<GraphNode, List<GraphNode>>(); } [JsonProperty("allGraphNodes")] KeyValuePair<GraphNode, List<GraphNode>> [] allGraphNodeArray { get { return allGraphNodes?.ToArray(); } set { allGraphNodes = allGraphNodes ?? new Dictionary<GraphNode, List<GraphNode>>(); if (value != null) foreach (var pair in value) allGraphNodes.Add(pair.Key, pair.Value); } } } [Serializable] [JsonObject(IsReference = true)] // Forces all instances to be serialized with reference preservation public class GraphNode { public enum Type { entrance = 0, enemy = 1, hub = 2, shop = 3, boss = 4 } public Type type; [JsonConstructor] // Constructor for serialization. GraphNode() { } public GraphNode(Type type) { SetType(type); } public void SetType(Type type) { this.type = type; } } 您将能够按如下方式进行序列化: var settings = new JsonSerializerSettings { // Add any settings as required, e.g. // Converters = { new StringEnumConverter() }, }; var json1 = JsonConvert.SerializeObject(gameData1, settings); var gameData2 = JsonConvert.DeserializeObject<GameData>(json1, settings); 演示小提琴在这里。

回答 1 投票 0

在 PostgreSQL 中将整数转换为字符串

如何将整数转换为字符串作为 PostgreSQL 查询的一部分? 因此,举例来说,我需要: SELECT * FROM table WHERE = '数字字符串' 可以在哪里

回答 5 投票 0

numpy 数组在写入文件时更改为字符串

我有一个数据框,其中一列是 numpy 数组: DF 名称 向量 0 阿贝纳基石-(Ce) [0.0, 0.0, 0.0, 0.0, 0.0, 0.043, 0.0, 0.478, 0... 1 阿伯纳西石 [0.0...

回答 2 投票 0

将字符串转换为二进制的最快方法?

我想使用字符串类将字符串转换为二进制。一个字符一个字符地执行此操作的快速方法是什么?环形?或者有什么函数可以为我转换吗? 1 和 0

回答 4 投票 0

用户定义的转换函数和转换为引用

我遇到以下代码的编译错误: 符号组类 { std::字符串 d_; 民众: SymbolGroup(std::string a):d_(a){} // 显式运算符 const std::string&() c...

回答 3 投票 0

为什么这个函数调用不明确,转换为 bool 需要额外的步骤?

考虑以下代码: 结构体Foo { typedef bool (*fptr_t)(int); 运算符 fptr_t() const { 返回_func; } fptr_t_func; }; 类包装器 { 民众: 包装纸(...

回答 1 投票 0

无需反序列化的 Google Protobuf

通常 google protobuf 或 nanopb 实现旨在分别在服务器端和客户端上序列化/反序列化消息。 但是,我面临着序列化的情况......

回答 1 投票 0

C++ 将向量<int>转换为向量<double>

将 std::vector intVec 转换为 std::vector doubleVec 的好干净方法是什么。或者,更一般地说,转换两个可转换类型的向量?

回答 3 投票 0

为什么可以使用两个 int 构造 std::vector?

vector有一个获取向量大小的构造函数,据我所知它是显式的,这可以通过以下代码无法编译的事实来证明 无效 f(std::vecto...

回答 3 投票 0

如何从浮点数复制值?浮动,如果浮动则置零?为空?

我有这个代码: StartPosLongitude = (object)time.StartPosition.Position.Long ?? 0 它编译时不会出现错误: 无法将类型“object”隐式转换为“float” 起始位置经度为

回答 4 投票 0

JSON转ModBus格式方便吗?

我目前正在寻找将数据从 JSON 格式转换为 Modbus 格式的方法,最好是易于在 Node-RED 中使用的 JS 脚本。我认为鉴于范围广泛,搜索不会花费很长时间

回答 1 投票 0

插入多个项目时将varchar转换为int数据类型时转换失败

我遇到过以下场景: 创建表测试 (Foo NVARCHAR(MAX)) 插入测试(Foo)值('1.0') 插入测试 (Foo) 值 (1) 两个插件都工作正常。但是当我们插入...

回答 1 投票 0

如何将记录转换为不同的记录类型,并且在 Ballerina 中某些字段是可选的?

我使用的许多 API 都期望只接收已填充的节点。如果节点的数据为空并且该节点是发布负载的一部分,则会返回错误。 例如,使用此来源

回答 1 投票 0

从字符串到“Double”的转换无效

更新的代码:按照建议打开 Option Strict On 后,我现在收到以下错误:对于 tbMonday1 和 tbMonday2 变量: 选项 strict On 不允许隐式转换...

回答 2 投票 0

如何转换或实现CvMatToSE3函数?

我为单目惯性ORB_SLAM3创建了一个节点。使用 ROS2 Humble 和 C++。该代码有一个问题,需要将 Cv::Mat 类型转换为 Sophus::SE3。这是转换应该的函数

回答 1 投票 0

.NET框架中的对象转换

我是 .NET 框架的新手。我在对象类型转换中遇到困难,我需要将一个类对象转换为另一个类对象。有人可以帮助我吗? 假设我有两个类,并且都是继承的...

回答 1 投票 0

如何在 C# 中从 List<int> 生成字节数组?

我正在尝试对一些未记录的旧代码进行逆向工程。我有一个 Base64 字符串,它被转换为字节数组,然后使用以下代码进行转换: 公共静态 IEnumerable<...

回答 1 投票 0

如何在c#中将字节数组转换为List<int>?

我正在尝试对一些旧数据进行逆向工程。数据库保存一个base64字符串,表示C#中int类型的List对象。当然可以很容易地转换为字节数组,但那就是

回答 1 投票 0

如何将 unicode 数字转换为 std::wstring?

有没有一种简单的方法可以将 Unicode 数字转换为 std::wstring? 例如我想将 U+1E9E (=16785054) 转换为 ẞ。

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.