Unity,JsonFX不反序列化

时间:2015-11-28 16:54:41

标签: c# json unity3d jsonfx

我只想序列化一些数据并将其反序列化,它必须在iOS平台上运行。 Json.NET不适用于iOS。 JsonFX可以序列化数据,字符串看起来像Json.Net序列化的字符串,但JsonFX不能反序列化数据。 这是简单的代码,它给我下一个错误

  

InvalidCastException:无法从源类型转换为目标类型。

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using Pathfinding.Serialization.JsonFx;

public class jsonTest : MonoBehaviour {

    public Text text;
    public List<ChapterModel> chapters;

    // Use this for initialization
    void Start () {
         chapters = new List<ChapterModel>();

         for(int i=0; i<2; i++)
         {
             var cm = new ChapterModel();
             cm.pages = new List<PageModel>();
             cm.id = i;

             for(int j =0; j<2; j++)
             {
                 var pm = new PageModel();
                 pm.BackgroundArt = j.ToString();
                 cm.pages.Add(pm);
             }

             chapters.Add(cm);
         }

        Debug.Log(JsonReader.Deserialize<List<ChapterModel>>(JsonWriter.Serialize(chapters)).Count);
    }
}

有谁知道如何解决它?

1 个答案:

答案 0 :(得分:0)

在与JsonFX玩了一下之后我发现使用它非常棘手:

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Linq;
using Pathfinding.Serialization.JsonFx;
using UnityEditor;

public class jsonTest : MonoBehaviour
{

public Text text;
public List<ChapterModel> chapters;

// Use this for initialization
void Start()
{
    chapters = new List<ChapterModel>();

    for (int i = 0; i < 2; i++)
    {
        var cm = new ChapterModel();
        cm.pages = new List<PageModel>();
        cm.id = i;

        for (int j = 0; j < 2; j++)
        {
            var pm = new PageModel();
            pm.BackgroundArt = j.ToString();
            cm.pages.Add(pm);
        }

        chapters.Add(cm);
    }

    string json = JsonWriter.Serialize(chapters);

    //Will end up in in invalid cast:
    //var deserialized = JsonReader.Deserialize<List<ChapterModel>(json);

    //Will be casted to object[] on deserialization:
    //var deserialized = JsonReader.Deserialize(json, typeof (List<ChapterModel>));

    //Will evaluate just fine:
    var deserialized = JsonReader.Deserialize(json, typeof(List<ChapterModel>)) as object[];
    List<ChapterModel> list = deserialized.Select(item => item as ChapterModel).ToList();
    Debug.Log(list.Count);
    }
}

[System.Serializable]
public class ChapterModel
{
   public int id = -1;
   public List<PageModel> pages;
}


[System.Serializable]
public class PageModel
{
   public string BackgroundArt,
    ForeAddedArt,
    VFXloop, VFXappeared, VFXnextSlide,
    BGAmbientLoop, BGMusicLoop, BGVoiceLoop,
    VFXSound, MusicSound, VoiceSound;
}

但是,如果我们让JSON知道typehint,那应该没问题。什么有趣,不是:

    //(...)
    StringBuilder output = new StringBuilder();

    JsonWriterSettings writerSettings = new JsonWriterSettings { TypeHintName = "__type" };

    JsonWriter writer = new JsonWriter(output, writerSettings);
    writer.Write(chapters);
    JsonReaderSettings readerSettings = new JsonReaderSettings { TypeHintName = "__type" };
    JsonReader reader = new JsonReader(output, readerSettings);

    //Will now see objects inside array as ChapterModel but still will fail to cast properly to list/array of ChapterModels
    List<ChapterModel> deserialized = reader.Deserialize() as List<ChapterModel>;
    Debug.Log(deserialized.Count);

你可能想要多玩一点,或者坚持使用上面提供的丑陋版本。