2023-09-08 17:45:47 -04:00
|
|
|
package funkin.data;
|
|
|
|
|
2023-09-25 23:24:07 -04:00
|
|
|
import funkin.util.SerializerUtil;
|
2023-10-18 01:02:10 -04:00
|
|
|
import thx.semver.Version;
|
|
|
|
import thx.semver.VersionRule;
|
2023-10-21 01:04:50 -04:00
|
|
|
import haxe.ds.Either;
|
2023-09-25 23:24:07 -04:00
|
|
|
|
2023-09-08 17:45:47 -04:00
|
|
|
/**
|
|
|
|
* `json2object` has an annotation `@:jcustomwrite` which allows for custom serialization of values to be written to JSON.
|
|
|
|
*
|
|
|
|
* Functions must be of the signature `(T) -> String`, where `T` is the type of the property.
|
2023-10-21 01:04:50 -04:00
|
|
|
*
|
|
|
|
* NOTE: Result must include quotation marks if the value is a string! json2object will not add them for you!
|
2023-09-08 17:45:47 -04:00
|
|
|
*/
|
2023-09-25 23:24:07 -04:00
|
|
|
class DataWrite
|
|
|
|
{
|
2023-10-18 01:02:10 -04:00
|
|
|
/**
|
|
|
|
* `@:jcustomwrite(funkin.data.DataWrite.dynamicValue)`
|
|
|
|
* @param value
|
|
|
|
* @return String
|
|
|
|
*/
|
2023-09-25 23:24:07 -04:00
|
|
|
public static function dynamicValue(value:Dynamic):String
|
|
|
|
{
|
|
|
|
// Is this cheating? Yes. Do I care? No.
|
|
|
|
return SerializerUtil.toJSON(value);
|
|
|
|
}
|
2023-10-18 01:02:10 -04:00
|
|
|
|
|
|
|
/**
|
2023-10-21 01:04:50 -04:00
|
|
|
*
|
2023-10-18 01:02:10 -04:00
|
|
|
* `@:jcustomwrite(funkin.data.DataWrite.semverVersion)`
|
|
|
|
*/
|
|
|
|
public static function semverVersion(value:Version):String
|
|
|
|
{
|
2023-10-21 01:04:50 -04:00
|
|
|
return '"${value.toString()}"';
|
2023-10-18 01:02:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `@:jcustomwrite(funkin.data.DataWrite.semverVersionRule)`
|
|
|
|
*/
|
|
|
|
public static function semverVersionRule(value:VersionRule):String
|
|
|
|
{
|
2023-10-21 01:04:50 -04:00
|
|
|
return '"${value.toString()}"';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `@:jcustomwrite(funkin.data.DataWrite.eitherFloatOrFloats)`
|
|
|
|
*/
|
|
|
|
public static function eitherFloatOrFloats(value:Null<Either<Float, Array<Float>>>):String
|
|
|
|
{
|
|
|
|
switch (value)
|
|
|
|
{
|
|
|
|
case null:
|
|
|
|
return '${1.0}';
|
|
|
|
case Left(inner):
|
|
|
|
return '$inner';
|
|
|
|
case Right(inner):
|
|
|
|
return dynamicValue(inner);
|
|
|
|
}
|
2023-10-18 01:02:10 -04:00
|
|
|
}
|
2023-09-25 23:24:07 -04:00
|
|
|
}
|