Unity jsonUtility序列化ListT,DictionaryT等泛型

2024-04-01 22:58

本文主要是介绍Unity jsonUtility序列化ListT,DictionaryT等泛型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文地址 https://blog.csdn.net/Truck_Truck/article/details/78292390

// Serialization.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;// List<T>
[Serializable]
public class Serialization<T>
{[SerializeField]List<T> target;public List<T> ToList() { return target; }public Serialization(List<T> target){this.target = target;}
}// Dictionary<TKey, TValue>
[Serializable]
public class Serialization<TKey, TValue> : ISerializationCallbackReceiver
{[SerializeField]List<TKey> keys;[SerializeField]List<TValue> values;Dictionary<TKey, TValue> target;public Dictionary<TKey, TValue> ToDictionary() { return target; }public Serialization(Dictionary<TKey, TValue> target){this.target = target;}public void OnBeforeSerialize(){keys = new List<TKey>(target.Keys);values = new List<TValue>(target.Values);}public void OnAfterDeserialize(){var count = Math.Min(keys.Count, values.Count);target = new Dictionary<TKey, TValue>(count);for (var i = 0; i < count; ++i){target.Add(keys[i], values[i]);}}
}

在这个思路的基础上封装了两个方法出来更方便使用

public class SerializeTools
{public static string ListToJson<T>(List<T> l) {return JsonUtility.ToJson(new Serialization<T>(l));}public static List<T> ListFromJson<T>(string str) {return JsonUtility.FromJson<Serialization<T>>(str).ToList();}public static string DicToJson<TKey, TValue>(Dictionary<TKey, TValue> dic){return JsonUtility.ToJson(new Serialization<TKey, TValue>(dic));}public static Dictionary<TKey, TValue> DicFromJson<TKey, TValue>(string str){return JsonUtility.FromJson<Serialization<TKey, TValue>>(str).ToDictionary();}}

这篇关于Unity jsonUtility序列化ListT,DictionaryT等泛型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/868349

相关文章

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

SpringBoot项目中Redis存储Session对象序列化处理

《SpringBoot项目中Redis存储Session对象序列化处理》在SpringBoot项目中使用Redis存储Session时,对象的序列化和反序列化是关键步骤,下面我们就来讲讲如何在Spri... 目录一、为什么需要序列化处理二、Spring Boot 集成 Redis 存储 Session2.1

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

C# Where 泛型约束的实现

《C#Where泛型约束的实现》本文主要介绍了C#Where泛型约束的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用的对象约束分类where T : structwhere T : classwhere T : ne

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

Django序列化中SerializerMethodField的使用详解

《Django序列化中SerializerMethodField的使用详解》:本文主要介绍Django序列化中SerializerMethodField的使用,具有很好的参考价值,希望对大家有所帮... 目录SerializerMethodField的基本概念使用SerializerMethodField的

Jackson库进行JSON 序列化时遇到了无限递归(Infinite Recursion)的问题及解决方案

《Jackson库进行JSON序列化时遇到了无限递归(InfiniteRecursion)的问题及解决方案》使用Jackson库进行JSON序列化时遇到了无限递归(InfiniteRecursi... 目录解决方案‌1. 使用 @jsonIgnore 忽略一个方向的引用2. 使用 @JsonManagedR