mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-08-01 00:08:05 -04:00
60 lines
1.3 KiB
C#
60 lines
1.3 KiB
C#
namespace Sandbox;
|
|
|
|
using System;
|
|
using System.Text.Json;
|
|
|
|
public partial class Doo : IJsonConvert
|
|
{
|
|
/// <summary>
|
|
/// Deserializes a Doo from JSON.
|
|
/// </summary>
|
|
public static object JsonRead( ref Utf8JsonReader reader, Type typeToConvert )
|
|
{
|
|
if ( reader.TokenType != JsonTokenType.StartObject )
|
|
throw new JsonException( "Expected StartObject" );
|
|
|
|
// Check if we're allowed to deserialize scripts, this is
|
|
// forbidden when reading JSON from untrusted sources (non-hosts)
|
|
|
|
Json.AssertCanDeserializeScripts();
|
|
|
|
var doo = new Doo();
|
|
|
|
while ( reader.Read() )
|
|
{
|
|
if ( reader.TokenType == JsonTokenType.EndObject )
|
|
break;
|
|
|
|
if ( reader.TokenType != JsonTokenType.PropertyName )
|
|
throw new JsonException( "Expected PropertyName" );
|
|
|
|
var propName = reader.GetString();
|
|
reader.Read();
|
|
|
|
switch ( propName )
|
|
{
|
|
case "body":
|
|
doo.Body = Json.Deserialize<List<Block>>( ref reader );
|
|
break;
|
|
default:
|
|
reader.Skip();
|
|
break;
|
|
}
|
|
}
|
|
|
|
return doo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes a Doo to JSON.
|
|
/// </summary>
|
|
public static void JsonWrite( object value, Utf8JsonWriter writer )
|
|
{
|
|
var doo = (Doo)value;
|
|
writer.WriteStartObject();
|
|
writer.WritePropertyName( "body" );
|
|
Json.Serialize( writer, doo.Body );
|
|
writer.WriteEndObject();
|
|
}
|
|
}
|