public override void WriteJson(JsonWriter writer, object value, JsonSerializerWriter internalWriter)
        {
            if (value == null || (value is UnityEngine.Object && value as UnityEngine.Object == null))
            {
                writer.WriteNull();
                return;
            }
            Type objectType = value.GetType();

            internalWriter._rootType  = objectType;
            internalWriter._rootLevel = internalWriter._serializeStack.Count + 1;
            JsonObjectContract contract = (JsonObjectContract)internalWriter.GetContractSafe(value);

            try
            {
                if (internalWriter.ShouldWriteReference(value, null, contract, null, null))
                {
                    internalWriter.WriteReference(writer, value);
                }
                else
                {
                    if (value == null)
                    {
                        writer.WriteNull();
                        return;
                    }
                    internalWriter.OnSerializing(writer, contract, value);

                    internalWriter._serializeStack.Add(value);

                    internalWriter.WriteObjectStart(writer, value, contract, null, null, null);

                    WriteProperties(contract, writer, value, objectType, internalWriter);

                    writer.WriteEndObject();

                    internalWriter._serializeStack.RemoveAt(internalWriter._serializeStack.Count - 1);

                    internalWriter.OnSerialized(writer, contract, value);
                }
            }
            catch (Exception ex)
            {
                if (internalWriter.IsErrorHandled(null, contract, null, null, writer.Path, ex))
                {
                    internalWriter.HandleError(writer, 0);
                }
                else
                {
                    // clear context in case serializer is being used inside a converter
                    // if the converter wraps the error then not clearing the context will cause this error:
                    // "Current error context error is different to requested error."
                    internalWriter.ClearErrorContext();
                    throw;
                }
            }
            finally
            {
                // clear root contract to ensure that if level was > 1 then it won't
                // accidently be used for non root values
                internalWriter._rootType = null;
            }
        }