Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace StrongExtensions
- {
- public static class JsonExtensions
- {
- private static readonly JsonSerializerSettings Settings = new()
- {
- Converters =
- {
- new ColorConverter(),
- new UnityObjectConverter(),
- new IntReactivePropertyJsonConverter(),
- new FloatReactivePropertyJsonConverter(),
- new StringReactivePropertyJsonConverter(),
- }
- };
- public static string ToJson<T>(this T target, bool prettyPrint = false)
- {
- Formatting formatting = prettyPrint
- ? Formatting.Indented
- : Formatting.None;
- return JsonConvert.SerializeObject(target, formatting, Settings);
- }
- public static T FromJson<T>(this string json) =>
- JsonConvert.DeserializeObject<T>(json, Settings);
- public static string ToPrettyJson<T>(this T target, string name = "") =>
- $"{name} : \n{JsonConvert.SerializeObject(target, Formatting.Indented, Settings)}";
- public static T LogAsJson<T>(this T value, string name = "")
- {
- Type type = typeof(T);
- if (name.IsNullOrEmpty())
- name = type.Name;
- string prettyJson = value.ToPrettyJson(name);
- Debug.Log(prettyJson);
- return value;
- }
- public static IEnumerable<T> LogAsJson<T>(this IEnumerable<T> source, string name = null)
- {
- IEnumerable<T> log = source as T[] ?? source.ToArray();
- Debug.Log(log.Count());
- if (name.IsNullOrEmpty())
- name = typeof(T).Name;
- Debug.Log(log.ToPrettyJson(name));
- return log;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement