2022-10-07 04:37:21 +00:00
|
|
|
package funkin.util;
|
|
|
|
|
|
|
|
import haxe.Json;
|
2023-05-30 20:21:24 +00:00
|
|
|
import haxe.io.Bytes;
|
2022-10-07 04:37:21 +00:00
|
|
|
import thx.semver.Version;
|
|
|
|
|
2023-03-18 19:47:23 +00:00
|
|
|
typedef ScoreInput =
|
|
|
|
{
|
|
|
|
var d:Int; // Key pressed
|
|
|
|
var l:Int; // Duration
|
|
|
|
var t:Int; // Start timestamp
|
|
|
|
}
|
|
|
|
|
2023-08-22 08:27:30 +00:00
|
|
|
/**
|
|
|
|
* A class of functions dedicated to serializing and deserializing data.
|
2023-09-26 03:24:07 +00:00
|
|
|
* TODO: Rewrite/refactor this to use json2object.
|
2023-08-22 08:27:30 +00:00
|
|
|
*/
|
2022-10-07 04:37:21 +00:00
|
|
|
class SerializerUtil
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
static final INDENT_CHAR = "\t";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a Haxe object to a JSON string.
|
2023-08-22 08:27:30 +00:00
|
|
|
*/
|
2023-08-28 19:03:29 +00:00
|
|
|
public static function toJSON(input:Dynamic, pretty:Bool = true):String
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
return Json.stringify(input, replacer, pretty ? INDENT_CHAR : null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a JSON string to a Haxe object.
|
|
|
|
*/
|
|
|
|
public static function fromJSON(input:String):Dynamic
|
|
|
|
{
|
2023-08-11 18:00:38 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return Json.parse(input);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
trace('An error occurred while parsing JSON from string data');
|
|
|
|
trace(e);
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 20:21:24 +00:00
|
|
|
/**
|
|
|
|
* Convert a JSON byte array to a Haxe object.
|
|
|
|
*/
|
|
|
|
public static function fromJSONBytes(input:Bytes):Dynamic
|
|
|
|
{
|
2023-06-10 07:59:25 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return Json.parse(input.toString());
|
|
|
|
}
|
|
|
|
catch (e:Dynamic)
|
|
|
|
{
|
|
|
|
trace('An error occurred while parsing JSON from byte data');
|
|
|
|
trace(e);
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-30 20:21:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Customize how certain types are serialized when converting to JSON.
|
|
|
|
*/
|
|
|
|
static function replacer(key:String, value:Dynamic):Dynamic
|
|
|
|
{
|
|
|
|
// Hacky because you can't use `isOfType` on a struct.
|
|
|
|
if (key == "version")
|
|
|
|
{
|
|
|
|
if (Std.isOfType(value, String)) return value;
|
|
|
|
|
|
|
|
// Stringify Version objects.
|
2023-08-22 08:27:30 +00:00
|
|
|
return serializeVersion(cast value);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Else, return the value as-is.
|
|
|
|
return value;
|
|
|
|
}
|
2023-08-22 08:27:30 +00:00
|
|
|
|
|
|
|
static inline function serializeVersion(value:thx.semver.Version):String
|
|
|
|
{
|
|
|
|
var result = '${value.major}.${value.minor}.${value.patch}';
|
|
|
|
if (value.hasPre) result += '-${value.pre}';
|
|
|
|
// TODO: Merge fix for version.hasBuild
|
|
|
|
if (value.build.length > 0) result += '+${value.build}';
|
|
|
|
return result;
|
|
|
|
}
|
2022-10-07 04:37:21 +00:00
|
|
|
}
|