What is the best method to save an array of arrays to a file (float[,][,])?
Thanks.
a way
using System;
using FullSerializer;
public static class StringSerializationAPI {
private static readonly fsSerializer _serializer = new fsSerializer();
public static string Serialize(Type type, object value) {
// serialize the data
fsData data;
_serializer.TrySerialize(type, value, out data).AssertSuccessWithoutWarnings();
// emit the data via JSON
return fsJsonPrinter.CompressedJson(data);
}
public static object Deserialize(Type type, string serializedState) {
// step 1: parse the JSON data
fsData data = fsJsonParser.Parse(serializedState);
// step 2: deserialize the data
object deserialized = null;
_serializer.TryDeserialize(data, type, ref deserialized).AssertSuccessWithoutWarnings();
return deserialized;
}
}
Tested here. Throws this exception on try to serialize:
ArgumentException: Only single dimension arrays are supported.
if you don’t need jagged you can represent a multidimensional with a single dimensional
but either wait it should work if you convert it to a list or array with array.ToList() or array.ToArray() (using System.Collection.Generic) first? but maybe have trouble deserialize?
here i fake serialize a dictionary by saving a List of KeyValuePairs mapping layer number to a list of tile ids
using basically that same code
public void Save()
{
#if UNITY_EDITOR && !UNITY_WEBPLAYER
string json = StringSerialization.Serialize<List<KeyValuePair<int, List<int>>>>(GetSaveData());
File.WriteAllText(EditorUtility.SaveFilePanel("Save JSON", Application.dataPath + "/../", "Map", "json"), json);
#endif
}
public static class StringSerialization
{
static readonly fsSerializer _serializer = new fsSerializer();
public static string Serialize<T>(T value)
{
fsData data;
var result = _serializer.TrySerialize<T>(value, out data);
if (result.Failed) throw new Exception(result.FormattedMessages);
return fsJsonPrinter.CompressedJson(data);
}
public static T Deserialize<T>(string serializedState)
{
fsResult result;
fsData data;
result = fsJsonParser.Parse(serializedState, out data);
if (result.Failed) throw new Exception(result.FormattedMessages);
T deserialized = default(T);
result = _serializer.TryDeserialize<T>(data, ref deserialized);
if (result.Failed) throw new Exception(result.FormattedMessages);
return deserialized;
}
}
JSON?