UE-数据序列化反序列化方案
<p>[TOC]</p>
<table>
<thead>
<tr>
<th>作者</th>
<th>赖俊贤、QFord</th>
</tr>
</thead>
<tbody>
<tr>
<td>创建日期</td>
<td>2024-6-26</td>
</tr>
<tr>
<td>更新日期</td>
<td>2024-6-26</td>
</tr>
</tbody>
</table>
<h1>背景</h1>
<p>虚拟实验室的Unreal版本与引擎、组件通信时需要按规则解析或者编写不同格式的数据,同时在功能开发的过程中也会产生自身的数据文件,这就需要在UE中实现不同格式数据的序列化和反序列化功能</p>
<h1>需求</h1>
<ol>
<li>调用磁学引擎接口创建磁体组件时参数要传Json数据,需要将结构体转换为Json字符串</li>
</ol>
<h1>Json</h1>
<h2>UE官方的Json库</h2>
<p><strong>源码位置</strong>
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=9e1062980645f49185787049c4b01892&amp;file=file.png" alt="" /></p>
<p><strong>存在问题</strong></p>
<ol>
<li>该Json库没有提供蓝图接口</li>
<li>有个很恶心的机制,会将Json数据字段首字母转换为小写,这导致与其他区分大小写的Json库(比如磁学引擎用的是C# litJson)不兼容
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=0af34ef2a1839493d7389fa37d70615f&amp;file=file.png" alt="" /></li>
</ol>
<h2>插件【Json Blueprint Utilities】</h2>
<p>为在蓝图中也能调用Json接口,引入了UE官方插件【Json Blueprint Utilities】
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=00ad5a3b77de8cab2cd60aaebe8b6858&amp;file=file.png" alt="" /></p>
<p>该插件基于官方Json库封装了一些蓝图接口
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=d7a4bdc7e180091204e9dc4b8a255bdb&amp;file=file.png" alt="" /></p>
<p>使用方法:[UE5_官方Json插件来了!](<a href="https://zhuanlan.zhihu.com/p/483455946">https://zhuanlan.zhihu.com/p/483455946</a> "UE5_官方Json插件来了!")</p>
<p><strong>存在问题</strong>
调用Convert Struct To Json String接口时也会存在转换后的字段首字母变小写</p>
<h2>插件【Blueprint Structure And JSON Conversion】</h2>
<p>为解决调用【Json Blueprint Utilities】Convert Struct To Json String接口转换后的字段首字母变小写的问题,引入了第三方插件【Blueprint Structure And JSON Conversion】
插件说明:[Blueprint Structure And JSON Conversion](<a href="https://www.showdoc.com.cn/virtualLabUnreal/11268065869492543">https://www.showdoc.com.cn/virtualLabUnreal/11268065869492543</a> "Blueprint Structure And JSON Conversion")
该插件提供两个接口
<img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=139ad365d0e36eacc06c92378d8a1e9a&amp;file=file.png" alt="" /></p>