All Projects → jilleJr → Newtonsoft.Json-for-Unity.Converters

jilleJr / Newtonsoft.Json-for-Unity.Converters

Licence: MIT license
Converters of common Unity types for Newtonsoft.Json. Goes hand in hand with jilleJr/Newtonsoft.Json-for-Unity

Programming Languages

C#
18002 projects
shell
77523 projects
powershell
5483 projects

Projects that are alternatives of or similar to Newtonsoft.Json-for-Unity.Converters

oyster-package-generator
Oyster is a package generator for Unity Package Manager (UPM). It generates a best standards Unity package with automated deployments via CLI. Focus on coding your package instead of deployments, changelogs, ect.
Stars: ✭ 18 (-87.76%)
Mutual labels:  upm, unity-package-manager
CSharpCompilerSettingsForUnity
Change the C# compiler (csc) used on your Unity project, as you like!
Stars: ✭ 208 (+41.5%)
Mutual labels:  unity3d-plugin, upm
SafeValues
A simple Unity library for cheating prevention
Stars: ✭ 75 (-48.98%)
Mutual labels:  unity3d-plugin
UnitySceneSwitcher
Editor extension for quick scene switching.
Stars: ✭ 38 (-74.15%)
Mutual labels:  upm
apaw
APAW. Arquitectura y Patrones Web. Máster en Ingeniería Web
Stars: ✭ 46 (-68.71%)
Mutual labels:  upm
collision-sound
Collision Sound aims to be an easy and powerful way of adding sound to object collisions in Unity
Stars: ✭ 21 (-85.71%)
Mutual labels:  unity3d-plugin
Android Screen Recorder Plugin
A simple android library project for record android screen in Unity
Stars: ✭ 28 (-80.95%)
Mutual labels:  unity3d-plugin
awesome-unity
A curated list of awesome Unity games! 🎮
Stars: ✭ 346 (+135.37%)
Mutual labels:  unity3d-plugin
UnityGlobalTextSystem
Allow the user to 'change' the default font in Unity from "Arial" to a font of their liking.
Stars: ✭ 21 (-85.71%)
Mutual labels:  unity3d-plugin
UnitySoundManager
Sound manager with 3 tracks, language system, pooling system, Fade in/out effects, EventTrigger system and more.
Stars: ✭ 55 (-62.59%)
Mutual labels:  unity3d-plugin
GizmosPlus
A Unity Package that provides additional GIzmo shapes and tools.
Stars: ✭ 35 (-76.19%)
Mutual labels:  unity-package-manager
unity-now
▲ Vercel Now plugin for Unity. Deploy WebGL builds with ease
Stars: ✭ 21 (-85.71%)
Mutual labels:  upm
BakingSheet
Easy datasheet management for C# and Unity. Supports Excel, Google Sheet, JSON and CSV format.
Stars: ✭ 144 (-2.04%)
Mutual labels:  upm
FPSCounter
FPS counter plugin for BepInEx - measure frame time and plugin performance
Stars: ✭ 27 (-81.63%)
Mutual labels:  unity3d-plugin
UnityTexture3DAtlasImportPipeline
A Texture3D Atlas Import Pipeline for Unity 2019.3 and newer.
Stars: ✭ 24 (-83.67%)
Mutual labels:  unity3d-plugin
UnityProminentColor
Tool to gather main colors of an image using Unity.
Stars: ✭ 40 (-72.79%)
Mutual labels:  unity3d-plugin
CosmosFramework
CosmosFramework is a lightweight plug-in Unity development framework . Has a rich Unity method extensions and toolchain. async/await syntax support, multi-network channel support.Long term support for this project
Stars: ✭ 176 (+19.73%)
Mutual labels:  unity-package-manager
com.xrtk.oculus
The Oculus platform components for the XRTK
Stars: ✭ 11 (-92.52%)
Mutual labels:  upm
EmlakProjesi
Emlak ilanları yayınlama ve yönetme sistemi
Stars: ✭ 12 (-91.84%)
Mutual labels:  newtonsoft-json
YLYRichText
a feature-rich, easy to use unity rich text plugin
Stars: ✭ 32 (-78.23%)
Mutual labels:  unity3d-plugin

Unity Converters for Newtonsoft.Json

openupm CircleCI Codacy grade Contributor Covenant

This package contains converters to and from common Unity types. Types such as Vector2, Vector3, Matrix4x4, Quaternions, Color, even ScriptableObject, and many, many more. (See the full compatibility table of all +50 supported Unity types)

Dependencies

Newtonsoft.Json packages

This package requires the Newtonsoft.Json.dll file to be present in your project. So it does not have to be used with my jillejr.newtonsoft.json-for-unity package! I recognize that there are too many Newtonsoft.Json forks and variants out there in the Unity eco-system.

This package can be combined with any of the following:

Since v3.0.1 of Unity's fork (of my fork) of Newtonsoft.Json, they are now promising a maintained version with official support by Unity's own dev team. See installation instructions here: Install official UPM package

Newtonsoft.Json versions

There's no hard linking towards a specific version. The package has been tested and works as-is with Newtonsoft.Json 10.0.3, 11.0.2, 12.0.3 and 13.0.1.

This package has not been tested towards Newtonsoft.Json versions older than v10.0.3, though the API has not changed much in a long time so it should be fine to use even as old versions as Json .NET v8.0.1 without any troubles.

If you have any troubles with using this package with a specific version of Newtonsoft.Json, then don't fray in opening an issue so we can resolve it.

Installation

OpenUPM OpenUPM icon

Add the jillejr.newtonsoft.json-for-unity.converters OpenUPM package:

openupm add jillejr.newtonsoft.json-for-unity.converters

Visit the jilleJr/Newtonsoft.Json-for-Unity/wiki to read more about installing/upgrading via OpenUPM.

Other

Visit the jilleJr/Newtonsoft.Json-for-Unity/wiki for installation:

What does it solve

A lot of Unity types causes self-referencing loops, such as the Vector3 type. While serializing the value new Vector3(0,1,0), Newtonsoft.Json will start writing:

{
  "x": 0,
  "y": 1,
  "z": 0,
  "normalized": {
    "x": 0,
    "y": 1,
    "z": 0,
    "normalized": {
      "x": 0,
      "y": 1,
      "z": 0,
      "normalized": {
        "x": 0,
        "y": 1,
        "z": 0,
        "normalized": {
          ...
        }
      }
    }
  }
}

and so on, until "recursion" error.. But there are also some types such as the UnityEngine.RandomState that has its state variables hidden.

The converters in this package takes care of these issues, as well as many more.

Sample error without this package

Given this piece of code:

using UnityEngine;
using Newtonsoft.Json;

public class NewBehaviour : MonoBehaviour {
    void Start() {
        var json = JsonConvert.SerializeObject(transform.position);
        Debug.Log("Position as JSON: " + json);
    }
}

Then the following is shown:

Error in Unity Console window

JsonSerializationException: Self referencing loop detected for property 'normalized' with type 'UnityEngine.Vector3'. Path 'normalized'.
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs:347)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContainerContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonProperty property, Newtonsoft.Json.Serialization.JsonContract& memberContract, System.Object& memberValue) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs:552)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs:486)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs:181)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonObjectContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract collectionContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs:486)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (Newtonsoft.Json.JsonWriter writer, System.Object value, Newtonsoft.Json.Serialization.JsonContract valueContract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerProperty) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs:181)
Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) (at /root/repo/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalWriter.cs:96)
Newtonsoft.Json.JsonSerializer.SerializeInternal (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) (at /root/repo/Src/Newtonsoft.Json/JsonSerializer.cs:1146)
Newtonsoft.Json.JsonSerializer.Serialize (Newtonsoft.Json.JsonWriter jsonWriter, System.Object value, System.Type objectType) (at /root/repo/Src/Newtonsoft.Json/JsonSerializer.cs:1046)
Newtonsoft.Json.JsonConvert.SerializeObjectInternal (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializer jsonSerializer) (at /root/repo/Src/Newtonsoft.Json/JsonConvert.cs:665)
Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) (at /root/repo/Src/Newtonsoft.Json/JsonConvert.cs:614)
Newtonsoft.Json.JsonConvert.SerializeObject (System.Object value) (at /root/repo/Src/Newtonsoft.Json/JsonConvert.cs:530)
Sample.Start () (at Assets/Sample.cs:18)

Sample with this package

Same NewBehaviour script as above, but just by adding this package you instead see:

Debug message in Unity Console window

Position as JSON: {"x":201.0,"y":219.5,"z":0.0}
UnityEngine.Debug:Log(Object)
Sample:Start() (at Assets/Sample.cs:19)

Configuring converters

This package automatically adds all its converters to the JsonConvert.DefaultSettings if that value has been left untouched.

If you want to only use a certain subset of the converters, or perhaps add in some of your own custom converters, then you have some options that will be explored just below here.

Default settings

  • Use the custom Unity contract resolver that looks for [SerializeField] attributes.

  • Use only some of the Newtonsoft.Json converters, namely:

  • Use all converters from this package.

  • Use all other converters defined outside the Newtonsoft.Json namespace.

Custom settings through code

You can override these defaults mentioned above in your code whenever you're serializing/deserializing. For example:

using Newtonsoft.Json;
using Newtonsoft.Json.UnityConverters;
using Newtonsoft.Json.UnityConverters.Math;

var settings = new JsonSerializerSettings {
    Converters = new [] {
        new Vector3Converter(),
        new StringEnumConverter(),
    },
    ContractResolver = new UnityTypeContractResolver(),
};

var myObjectINeedToSerialize = new Vector3(1, 2, 3);

var json = JsonConvert.SerializeObject(myObjectINeedToSerialize, settings);

Custom settings through config file

Since v1.1.0 of this package, you can configure and override these defaults through a ScriptableObject that is saved at Assets/Resources/Newtonsoft.Json-for-Unity.Converters.asset.

To open the settings, click "Edit" in the top menu bar and select "Json.NET converters settings..."

Configuring ScriptableObject config

Within this settings page, you can enable or disable any of the converters you wish to include or omit by default.

These settings are only applied when you're using the default JsonSerializerSettings that this package has overridden. If you're setting the JsonSerializerSettings manually through code, as shown in the example above, then all of these settings will be ignored.

Contributing

Thankful that you're even reading this :)

If you want to contribute, here's what you can do:

  • Spread the word! More users → more feedback → I get more will-power to work on this project. This is the best way to contribute!

  • Open an issue. Could be a feature request for a new converter, or maybe you've found a bug?

  • Tackle one of the unassigned issues. If it looks like a fun task to solve and no one is assigned, then just comment on it and say that you would like to try it out.

  • Open a PR with some new feature or issue solved. Remember to ask before starting to work on anything, so no two are working on the same thing.

    Having a feature request or issue pop up and having the submitter suggesting themselves to later add a PR for a solution is the absolute greatest gift a repository maintainer could ever receive. 🎁

Changelog

Please see the CHANGELOG.md file inside this package.


This package is licensed under The MIT License (MIT)

Copyright (c) 2019 Kalle Fagerberg (jilleJr)
https://github.com/jilleJr/Newtonsoft.Json-for-Unity.Converters

See full copyrights in LICENSE.md inside repository

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].