├── docs
├── graph.xlsx
├── images.pptx
├── nuget.exe
├── pack.bat
├── push.bat
├── Utf8Json.AspNetCoreMvcFormatter.nuspec
├── Utf8Json.UnityShims.nuspec
├── Utf8Json.ImmutableCollection.nuspec
└── Utf8Json.nuspec
├── opensource.snk
├── src
├── Utf8Json.UnityClient
│ ├── Assets
│ │ └── Scripts
│ │ │ ├── Utf8Json
│ │ │ ├── Internal
│ │ │ ├── Resolvers
│ │ │ ├── Formatters
│ │ │ ├── Attributes.cs
│ │ │ ├── JsonReader.cs
│ │ │ ├── JsonToken.cs
│ │ │ ├── JsonWriter.cs
│ │ │ ├── IJsonFormatter.cs
│ │ │ ├── JsonSerializer.cs
│ │ │ ├── IJsonFormatterResolver.cs
│ │ │ ├── JsonSerializer.NonGeneric.cs
│ │ │ └── Unity
│ │ │ │ └── ResolverAndFormatters.cs
│ │ │ └── Tests
│ │ │ ├── PirvateTest.cs
│ │ │ ├── _Loader.cs
│ │ │ └── SimpleTest.cs
│ ├── ProjectSettings
│ │ ├── ProjectVersion.txt
│ │ ├── ClusterInputManager.asset
│ │ ├── PresetManager.asset
│ │ ├── EditorBuildSettings.asset
│ │ ├── NetworkManager.asset
│ │ ├── TimeManager.asset
│ │ ├── AudioManager.asset
│ │ ├── EditorSettings.asset
│ │ ├── TagManager.asset
│ │ ├── DynamicsManager.asset
│ │ ├── UnityConnectSettings.asset
│ │ ├── Physics2DSettings.asset
│ │ ├── NavMeshAreas.asset
│ │ └── GraphicsSettings.asset
│ ├── UnityPackageManager
│ │ └── manifest.json
│ └── Utf8Json.UnityClient.sln
├── Utf8Json
│ ├── opensource.snk
│ ├── Internal
│ │ ├── StringEncoding.cs
│ │ ├── FuncExtensions.cs
│ │ ├── ArrayBuffer.cs
│ │ ├── ArrayPool.cs
│ │ ├── StringMutator.cs
│ │ ├── UnsafeMemory.tt
│ │ ├── Emit
│ │ │ ├── DynamicAssembly.cs
│ │ │ └── ExpressionUtility.cs
│ │ ├── ReflectionExtensions.cs
│ │ ├── BinaryUtil.cs
│ │ └── DoubleConversion
│ │ │ └── DiyFp.cs
│ ├── _AssemblyInfo.cs
│ ├── Formatters
│ │ ├── IgnoreFormatter.cs
│ │ ├── AnonymousFormatter.cs
│ │ ├── NullableFormatter.cs
│ │ ├── DynamicObjectTypeFallbackFormatter.cs
│ │ ├── TupleFormatter.tt
│ │ └── ValueTupleFormatter.tt
│ ├── JsonToken.cs
│ ├── Attributes.cs
│ ├── IJsonFormatter.cs
│ ├── IJsonFormatterResolver.cs
│ └── Resolvers
│ │ ├── AttributeFormatterResolver.cs
│ │ └── EnumResolver.cs
├── Utf8Json.UnityShims
│ ├── opensource.snk
│ ├── _AssemblyInfo.cs
│ ├── Utf8Json.UnityShims.csproj
│ └── Shims.cs
├── Utf8Json.ImmutableCollection
│ ├── opensource.snk
│ ├── _AssemblyInfo.cs
│ ├── Utf8Json.ImmutableCollection.csproj
│ └── ImmutableCollectionResolver.cs
├── Utf8Json.AspNetCoreMvcFormatter
│ ├── opensource.snk
│ ├── _AssemblyInfo.cs
│ ├── Utf8Json.AspNetCoreMvcFormatter.csproj
│ └── Formatter.cs
├── Utf8Json.UniversalCodeGenerator
│ ├── Shims
│ │ └── CodeDom.cs
│ ├── Utf8Json.UniversalCodeGenerator.csproj
│ ├── Generator
│ │ └── TemplatePartials.cs
│ └── CodeAnalysis
│ │ └── Definitions.cs
└── Utf8Json.CodeGenerator
│ ├── Generator
│ ├── TemplatePartials.cs
│ ├── ResolverTemplate.tt
│ └── FormatterTemplate.tt
│ ├── Program.cs
│ ├── Properties
│ └── AssemblyInfo.cs
│ ├── App.config
│ ├── Definitions.cs
│ └── packages.config
├── sandbox
├── DynamicCodeDumper
│ ├── App.config
│ ├── Properties
│ │ └── AssemblyInfo.cs
│ └── DynamicCodeDumper.csproj
├── ConsoleAppNet461
│ ├── App.config
│ └── Properties
│ │ └── AssemblyInfo.cs
├── ConsoleAppNetCore
│ └── ConsoleAppNetCore.csproj
└── PerfBenchmark
│ ├── Program.cs
│ ├── PerfBenchmark.csproj
│ ├── Benchmark2.cs
│ └── TargetClass.cs
├── tests
└── Utf8Json.Tests
│ ├── TypeFormatterTest.cs
│ ├── ExpandoObjectTest.cs
│ ├── AnonymousTypeTest.cs
│ ├── StringEscapeTest.cs
│ ├── PrimitiveObjectFormatterTest.cs
│ ├── AsyncTest.cs
│ ├── BassClassDeserializeTest.cs
│ ├── WithBomTest.cs
│ ├── DecimalTest.cs
│ ├── Utf8Json.Tests.csproj
│ ├── CommentTest.cs
│ ├── GenericCustomFormatterTest.cs
│ ├── CompositeResolverTest.cs
│ ├── SkipReadTest.cs
│ ├── ValueTupleTest.cs
│ ├── ExcludeNullTest.cs
│ ├── DynamicObjectFallback.cs
│ ├── DataContractTest.cs
│ ├── ReadIsInTest.cs
│ ├── MultiDimentionalArrayTest.cs
│ ├── DictionaryTest.cs
│ ├── Utils
│ └── ChainingAssertion.Ext.cs
│ ├── StreamNonMemoryStraemDeserialization.cs
│ ├── ExceptionTest.cs
│ ├── NonGenericTest.cs
│ ├── GenericFormatter.cs
│ ├── FormatterAttributeTest.cs
│ ├── NonGenericCollectionTest.cs
│ ├── ReadNextBlockSegmentTest.cs
│ └── DoubleConversionTest.cs
├── LICENSE
└── make_unity_symlink.bat
/docs/graph.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/docs/graph.xlsx
--------------------------------------------------------------------------------
/docs/images.pptx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/docs/images.pptx
--------------------------------------------------------------------------------
/docs/nuget.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/docs/nuget.exe
--------------------------------------------------------------------------------
/opensource.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/opensource.snk
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/Internal:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/Internal
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/Resolvers:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/Resolvers
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/Formatters:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/Formatters
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/ProjectVersion.txt:
--------------------------------------------------------------------------------
1 | m_EditorVersion: 2018.1.0b2
2 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/Attributes.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/Attributes.cs
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/JsonReader.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/JsonReader.cs
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/JsonToken.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/JsonToken.cs
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/JsonWriter.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/JsonWriter.cs
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/UnityPackageManager/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "dependencies": {
3 | }
4 | }
5 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/IJsonFormatter.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/IJsonFormatter.cs
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/JsonSerializer.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/JsonSerializer.cs
--------------------------------------------------------------------------------
/src/Utf8Json/opensource.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/src/Utf8Json/opensource.snk
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/IJsonFormatterResolver.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/IJsonFormatterResolver.cs
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/JsonSerializer.NonGeneric.cs:
--------------------------------------------------------------------------------
1 | ../../../../Utf8Json/JsonSerializer.NonGeneric.cs
--------------------------------------------------------------------------------
/src/Utf8Json.UnityShims/opensource.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/src/Utf8Json.UnityShims/opensource.snk
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Utf8Json/Unity/ResolverAndFormatters.cs:
--------------------------------------------------------------------------------
1 | ../../../../../Utf8Json.UnityShims/ResolverAndFormatters.cs
--------------------------------------------------------------------------------
/src/Utf8Json.ImmutableCollection/opensource.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/src/Utf8Json.ImmutableCollection/opensource.snk
--------------------------------------------------------------------------------
/src/Utf8Json.AspNetCoreMvcFormatter/opensource.snk:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/neuecc/Utf8Json/HEAD/src/Utf8Json.AspNetCoreMvcFormatter/opensource.snk
--------------------------------------------------------------------------------
/docs/pack.bat:
--------------------------------------------------------------------------------
1 | nuget pack Utf8Json.nuspec
2 | nuget pack Utf8Json.ImmutableCollection.nuspec
3 | nuget pack Utf8Json.UnityShims.nuspec
4 | nuget pack Utf8Json.AspNetCoreMvcFormatter.nuspec
5 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/ClusterInputManager.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!236 &1
4 | ClusterInputManager:
5 | m_ObjectHideFlags: 0
6 | m_Inputs: []
7 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/PresetManager.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!1386491679 &1
4 | PresetManager:
5 | m_ObjectHideFlags: 0
6 | m_DefaultList: []
7 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/EditorBuildSettings.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!1045 &1
4 | EditorBuildSettings:
5 | m_ObjectHideFlags: 0
6 | serializedVersion: 2
7 | m_Scenes: []
8 |
--------------------------------------------------------------------------------
/sandbox/DynamicCodeDumper/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/NetworkManager.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!149 &1
4 | NetworkManager:
5 | m_ObjectHideFlags: 0
6 | m_DebugLevel: 0
7 | m_Sendrate: 15
8 | m_AssetToPrefab: {}
9 |
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/StringEncoding.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 |
3 | namespace Utf8Json.Internal
4 | {
5 | internal static class StringEncoding
6 | {
7 | public static readonly Encoding UTF8 = new UTF8Encoding(false);
8 | }
9 | }
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/TimeManager.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!5 &1
4 | TimeManager:
5 | m_ObjectHideFlags: 0
6 | Fixed Timestep: 0.02
7 | Maximum Allowed Timestep: 0.33333334
8 | m_TimeScale: 1
9 | Maximum Particle Timestep: 0.03
10 |
--------------------------------------------------------------------------------
/docs/push.bat:
--------------------------------------------------------------------------------
1 | nuget push Utf8Json.1.3.7.nupkg -Source https://www.nuget.org/api/v2/package
2 | nuget push Utf8Json.ImmutableCollection.1.3.7.nupkg -Source https://www.nuget.org/api/v2/package
3 | nuget push Utf8Json.UnityShims.1.3.7.nupkg -Source https://www.nuget.org/api/v2/package
4 | nuget push Utf8Json.AspNetCoreMvcFormatter.1.3.7.nupkg -Source https://www.nuget.org/api/v2/package
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/AudioManager.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!11 &1
4 | AudioManager:
5 | m_ObjectHideFlags: 0
6 | m_Volume: 1
7 | Rolloff Scale: 1
8 | Doppler Factor: 1
9 | Default Speaker Mode: 2
10 | m_SampleRate: 0
11 | m_DSPBufferSize: 0
12 | m_VirtualVoiceCount: 512
13 | m_RealVoiceCount: 32
14 | m_SpatializerPlugin:
15 | m_AmbisonicDecoderPlugin:
16 | m_DisableAudio: 0
17 | m_VirtualizeEffects: 1
18 |
--------------------------------------------------------------------------------
/src/Utf8Json.UniversalCodeGenerator/Shims/CodeDom.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace System.CodeDom.Compiler
6 | {
7 | public class CompilerError
8 | {
9 | public string ErrorText { get; set; }
10 | public bool IsWarning { get; set; }
11 | }
12 |
13 | public class CompilerErrorCollection
14 | {
15 | public void Add(CompilerError error)
16 | {
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/TypeFormatterTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class TypeFormatterTest
9 | {
10 | [Fact]
11 | public void TypeTest()
12 | {
13 | var bin = JsonSerializer.Serialize(typeof(int));
14 | var t = JsonSerializer.Deserialize(bin);
15 | (t == typeof(int)).IsTrue();
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/Utf8Json/_AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | [assembly: AssemblyTitle("Utf8Json")]
6 | [assembly: AssemblyDescription("")]
7 | [assembly: AssemblyConfiguration("")]
8 | [assembly: AssemblyCompany("")]
9 | [assembly: AssemblyTrademark("")]
10 | [assembly: AssemblyCulture("")]
11 |
12 | [assembly: ComVisible(false)]
13 |
14 | [assembly: Guid("929e3779-4c3a-42a3-bc72-3d77511e988f")]
15 | [assembly: AssemblyVersion("1.3.7")]
16 | [assembly: AssemblyFileVersion("1.3.7")]
17 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/EditorSettings.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!159 &1
4 | EditorSettings:
5 | m_ObjectHideFlags: 0
6 | serializedVersion: 4
7 | m_ExternalVersionControlSupport: Visible Meta Files
8 | m_SerializationMode: 2
9 | m_DefaultBehaviorMode: 1
10 | m_SpritePackerMode: 4
11 | m_SpritePackerPaddingPower: 1
12 | m_ProjectGenerationIncludedExtensions: txt;xml;fnt;cd
13 | m_ProjectGenerationRootNamespace:
14 | m_UserGeneratedProjectSuffix:
15 | m_CollabEditorSettings:
16 | inProgressEnabled: 1
17 |
--------------------------------------------------------------------------------
/src/Utf8Json/Formatters/IgnoreFormatter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Utf8Json.Formatters
4 | {
5 | public sealed class IgnoreFormatter : IJsonFormatter
6 | {
7 | public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
8 | {
9 | writer.WriteNull();
10 | }
11 |
12 | public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
13 | {
14 | reader.ReadNextBlock();
15 | return default(T);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityShims/_AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | [assembly: AssemblyTitle("Utf8Json.UnityShims")]
6 | [assembly: AssemblyDescription("")]
7 | [assembly: AssemblyConfiguration("")]
8 | [assembly: AssemblyCompany("")]
9 | [assembly: AssemblyTrademark("")]
10 | [assembly: AssemblyCulture("")]
11 |
12 | [assembly: ComVisible(false)]
13 |
14 | [assembly: Guid("bf5d0a42-70b7-417d-af8c-2f7e073057e2")]
15 | [assembly: AssemblyVersion("1.3.7")]
16 | [assembly: AssemblyFileVersion("1.3.7")]
--------------------------------------------------------------------------------
/src/Utf8Json.ImmutableCollection/_AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | [assembly: AssemblyTitle("Utf8Json.ImmutableCollection")]
6 | [assembly: AssemblyDescription("")]
7 | [assembly: AssemblyConfiguration("")]
8 | [assembly: AssemblyCompany("")]
9 | [assembly: AssemblyTrademark("")]
10 | [assembly: AssemblyCulture("")]
11 |
12 | [assembly: ComVisible(false)]
13 |
14 | [assembly: Guid("2e7ac239-3db1-4772-9f47-5f62abb56ed0")]
15 | [assembly: AssemblyVersion("1.3.7")]
16 | [assembly: AssemblyFileVersion("1.3.7")]
--------------------------------------------------------------------------------
/src/Utf8Json.AspNetCoreMvcFormatter/_AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | [assembly: AssemblyTitle("Utf8Json.AspNetCoreMvcFormatter")]
6 | [assembly: AssemblyDescription("")]
7 | [assembly: AssemblyConfiguration("")]
8 | [assembly: AssemblyCompany("")]
9 | [assembly: AssemblyTrademark("")]
10 | [assembly: AssemblyCulture("")]
11 |
12 | [assembly: ComVisible(false)]
13 |
14 | [assembly: Guid("899f8b6c-c4c7-4e8b-9875-f61fdfb630bd")]
15 | [assembly: AssemblyVersion("1.3.7")]
16 | [assembly: AssemblyFileVersion("1.3.7")]
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/TagManager.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!78 &1
4 | TagManager:
5 | serializedVersion: 2
6 | tags: []
7 | layers:
8 | - Default
9 | - TransparentFX
10 | - Ignore Raycast
11 | -
12 | - Water
13 | - UI
14 | -
15 | -
16 | -
17 | -
18 | -
19 | -
20 | -
21 | -
22 | -
23 | -
24 | -
25 | -
26 | -
27 | -
28 | -
29 | -
30 | -
31 | -
32 | -
33 | -
34 | -
35 | -
36 | -
37 | -
38 | -
39 | -
40 | m_SortingLayers:
41 | - name: Default
42 | uniqueID: 0
43 | locked: 0
44 |
--------------------------------------------------------------------------------
/src/Utf8Json.ImmutableCollection/Utf8Json.ImmutableCollection.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;NET45;NET47
5 | true
6 | opensource.snk
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/Utf8Json.AspNetCoreMvcFormatter/Utf8Json.AspNetCoreMvcFormatter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;NET47
5 | true
6 | opensource.snk
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/src/Utf8Json.CodeGenerator/Generator/TemplatePartials.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Utf8Json.CodeGenerator.Generator
8 | {
9 | public partial class FormatterTemplate
10 | {
11 | public string Namespace;
12 | public ObjectSerializationInfo[] objectSerializationInfos;
13 | }
14 |
15 | public partial class ResolverTemplate
16 | {
17 | public string Namespace;
18 | public string FormatterNamespace { get; set; }
19 | public string ResolverName = "GeneratedResolver";
20 | public IResolverRegisterInfo[] registerInfos;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Utf8Json.UniversalCodeGenerator/Utf8Json.UniversalCodeGenerator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 | win-x64;linux-x64;osx-x64
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/ExpandoObjectTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Dynamic;
4 | using System.Text;
5 | using Xunit;
6 |
7 | namespace Utf8Json.Tests
8 | {
9 | public class ExpandoObjectTest
10 | {
11 | [Fact]
12 | public void Hoge()
13 | {
14 | dynamic expando = new ExpandoObject();
15 | expando.Hoge = "hogehoge";
16 | expando.Huga = "huga-";
17 |
18 | var j = Utf8Json.JsonSerializer.Serialize(expando);
19 |
20 | dynamic exp = Utf8Json.JsonSerializer.Deserialize(j);
21 | (exp.Hoge as string).Is("hogehoge");
22 | (exp.Huga as string).Is("huga-");
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/Utf8Json.UniversalCodeGenerator/Generator/TemplatePartials.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using Utf8Json.UniversalCodeGenerator;
7 |
8 | namespace Utf8Json.CodeGenerator.Generator
9 | {
10 | public partial class FormatterTemplate
11 | {
12 | public string Namespace;
13 | public ObjectSerializationInfo[] objectSerializationInfos;
14 | }
15 |
16 | public partial class ResolverTemplate
17 | {
18 | public string Namespace;
19 | public string FormatterNamespace { get; set; }
20 | public string ResolverName = "GeneratedResolver";
21 | public IResolverRegisterInfo[] registerInfos;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/AnonymousTypeTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Utf8Json.Resolvers;
5 | using Xunit;
6 |
7 | namespace Utf8Json.Tests
8 | {
9 | public class AnonymousTypeTest
10 | {
11 | [Fact]
12 | public void SerializeAndToJson()
13 | {
14 | var testData = new { Hoge = 100, Huga = true, Yaki = new { Rec = 1, T = 10 }, Nano = "nanoanno" };
15 | var ok = @"{""Hoge"":100,""Huga"":true,""Yaki"":{""Rec"":1,""T"":10},""Nano"":""nanoanno""}";
16 |
17 | JsonSerializer.ToJsonString(testData, StandardResolver.Default).Is(ok);
18 | JsonSerializer.ToJsonString(testData, StandardResolver.AllowPrivate).Is(ok);
19 | JsonSerializer.ToJsonString(testData, StandardResolver.ExcludeNull).Is(ok);
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/sandbox/ConsoleAppNet461/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/FuncExtensions.cs:
--------------------------------------------------------------------------------
1 | #if NETSTANDARD
2 |
3 | using System;
4 |
5 | namespace Utf8Json.Internal
6 | {
7 | // Unity compiler can't understand this.
8 |
9 | internal static class FuncExtensions
10 | {
11 | // hack of avoid closure allocation(() => value).
12 | public static Func AsFunc(this T value)
13 | {
14 | return new Func(value.ReturnBox);
15 | }
16 |
17 | public static Func AsFuncFast(this T value) where T : class
18 | {
19 | return new Func(value.Return);
20 | }
21 |
22 | static T Return(this T value)
23 | {
24 | return value;
25 | }
26 |
27 | static T ReturnBox(this object value)
28 | {
29 | return (T)value;
30 | }
31 | }
32 | }
33 |
34 |
35 | #endif
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/StringEscapeTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class StringEscapeTest
9 | {
10 | [Fact]
11 | public void Mixed()
12 | {
13 | var str = @"""\u0428\u0440\u0438-\u041b\u0430\u043d\u043a\u0430""";
14 | JsonSerializer.Deserialize(str).Is("Шри-Ланка");
15 | str = @"""\u041d\u043e\u0432\u0430\u044f \u0437\u0435\u043b\u0430\u043d\u0434\u0438\u044f""";
16 | JsonSerializer.Deserialize(str).Is("Новая зеландия");
17 |
18 |
19 | str = @"""\u041d\u043e\u0432\u0430\u044f___\u0437\u0435\u043b\u0430\t\u043d\u0434\u0438\u044f""";
20 | JsonSerializer.Deserialize(str).Is("Новая___зела\tндия");
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/DynamicsManager.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!55 &1
4 | PhysicsManager:
5 | m_ObjectHideFlags: 0
6 | serializedVersion: 3
7 | m_Gravity: {x: 0, y: -9.81, z: 0}
8 | m_DefaultMaterial: {fileID: 0}
9 | m_BounceThreshold: 2
10 | m_SleepThreshold: 0.005
11 | m_DefaultContactOffset: 0.01
12 | m_DefaultSolverIterations: 6
13 | m_DefaultSolverVelocityIterations: 1
14 | m_QueriesHitBackfaces: 0
15 | m_QueriesHitTriggers: 1
16 | m_EnableAdaptiveForce: 0
17 | m_EnablePCM: 1
18 | m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
19 | m_AutoSimulation: 1
20 | m_AutoSyncTransforms: 1
21 |
--------------------------------------------------------------------------------
/src/Utf8Json.CodeGenerator/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.IO;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Utf8Json.CodeGenerator
10 | {
11 | class Program
12 | {
13 | static void Main(string[] args)
14 | {
15 |
16 | }
17 |
18 | static void Output(string path, string text)
19 | {
20 | path = path.Replace("global::", "");
21 |
22 | const string prefix = "[Out]";
23 | Console.WriteLine(prefix + path);
24 |
25 | var fi = new FileInfo(path);
26 | if (!fi.Directory.Exists)
27 | {
28 | fi.Directory.Create();
29 | }
30 |
31 | System.IO.File.WriteAllText(path, text, Encoding.UTF8);
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Tests/PirvateTest.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using RuntimeUnitTestToolkit;
3 | using System.Collections;
4 |
5 | using System.Collections.Generic;
6 | using System;
7 | using Utf8Json;
8 | using System.Text;
9 | using System.Linq;
10 |
11 | namespace Utf8Json.UnityClient.Tests
12 | {
13 | public class BassPrivate
14 | {
15 | int x = 0;
16 |
17 | public int GetX()
18 | {
19 | return x;
20 | }
21 | }
22 |
23 | public class Inherit: BassPrivate
24 | {
25 | public int Y;
26 | }
27 |
28 | public class PrivateTest
29 | {
30 | public void CanSerialize()
31 | {
32 | var foo = JsonSerializer.Deserialize("{\"x\":10,\"Y\":99}", Utf8Json.Resolvers.StandardResolver.AllowPrivate);
33 | foo.Y.Is(99);
34 | foo.GetX().Is(10);
35 | }
36 | }
37 | }
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/ArrayBuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Utf8Json.Internal
4 | {
5 | public struct ArrayBuffer
6 | {
7 | public T[] Buffer;
8 | public int Size;
9 |
10 | public ArrayBuffer(int initialSize)
11 | {
12 | this.Buffer = new T[initialSize];
13 | this.Size = 0;
14 | }
15 |
16 | public void Add(T value)
17 | {
18 | if (Size >= this.Buffer.Length)
19 | {
20 | Array.Resize(ref Buffer, Size * 2);
21 | }
22 |
23 | Buffer[Size++] = value;
24 | }
25 |
26 | public T[] ToArray()
27 | {
28 | if (Buffer.Length == Size)
29 | {
30 | return Buffer;
31 | }
32 |
33 | var result = new T[Size];
34 | Array.Copy(Buffer, result, Size);
35 | return result;
36 | }
37 | }
38 | }
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/PrimitiveObjectFormatterTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Utf8Json.Formatters;
5 | using Xunit;
6 |
7 | namespace Utf8Json.Tests
8 | {
9 | public class PrimitiveObjectFormatterTest
10 | {
11 | [Fact]
12 | public void Collection()
13 | {
14 | var jw = new JsonWriter();
15 | PrimitiveObjectFormatter.Default.Serialize(ref jw, new[] { "foo", "bar", "baz" }, null);
16 |
17 | jw.ToString().Is("[\"foo\",\"bar\",\"baz\"]");
18 | }
19 |
20 | [Fact]
21 | public void Map()
22 | {
23 | var jw = new JsonWriter();
24 | PrimitiveObjectFormatter.Default.Serialize(ref jw, new Dictionary { { "foo", 10 }, { "bar", 99 }, { "baz", 100 } }, null);
25 |
26 | jw.ToString().Is("{\"foo\":10,\"bar\":99,\"baz\":100}");
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/UnityConnectSettings.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!310 &1
4 | UnityConnectSettings:
5 | m_ObjectHideFlags: 0
6 | m_Enabled: 0
7 | m_TestMode: 0
8 | m_TestEventUrl:
9 | m_TestConfigUrl:
10 | m_TestInitMode: 0
11 | CrashReportingSettings:
12 | m_EventUrl: https://perf-events.cloud.unity3d.com/api/events/crashes
13 | m_Enabled: 0
14 | m_CaptureEditorExceptions: 1
15 | UnityPurchasingSettings:
16 | m_Enabled: 0
17 | m_TestMode: 0
18 | UnityAnalyticsSettings:
19 | m_Enabled: 0
20 | m_InitializeOnStartup: 1
21 | m_TestMode: 0
22 | m_TestEventUrl:
23 | m_TestConfigUrl:
24 | UnityAdsSettings:
25 | m_Enabled: 0
26 | m_InitializeOnStartup: 1
27 | m_TestMode: 0
28 | m_EnabledPlatforms: 4294967295
29 | m_IosGameId:
30 | m_AndroidGameId:
31 | m_GameIds: {}
32 | m_GameId:
33 | PerformanceReportingSettings:
34 | m_Enabled: 0
35 |
--------------------------------------------------------------------------------
/src/Utf8Json/JsonToken.cs:
--------------------------------------------------------------------------------
1 | namespace Utf8Json
2 | {
3 | // 0 = None, 1 ~ 4 is block token, 5 ~ 9 = value token, 10 ~ 11 = delimiter token
4 | // you can use range-check if optimization needed.
5 |
6 | public enum JsonToken : byte
7 | {
8 | None = 0,
9 | /// {
10 | BeginObject = 1,
11 | /// }
12 | EndObject = 2,
13 | /// [
14 | BeginArray = 3,
15 | /// ]
16 | EndArray = 4,
17 | /// 0~9, -
18 | Number = 5,
19 | /// "
20 | String = 6,
21 | /// t
22 | True = 7,
23 | /// f
24 | False = 8,
25 | /// n
26 | Null = 9,
27 | /// ,
28 | ValueSeparator = 10,
29 | /// :
30 | NameSeparator = 11
31 | }
32 | }
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/AsyncTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using Xunit;
7 |
8 | namespace Utf8Json.Tests
9 | {
10 | public class AsyncTest
11 | {
12 | [Fact]
13 | public async Task Generic()
14 | {
15 | var ms = new MemoryStream();
16 | await Utf8Json.JsonSerializer.SerializeAsync(ms, 100);
17 | ms.Position = 0;
18 | var i = await Utf8Json.JsonSerializer.DeserializeAsync(ms);
19 | i.Is(100);
20 | }
21 |
22 | [Fact]
23 | public async Task NonGeneric()
24 | {
25 | var ms = new MemoryStream();
26 | await Utf8Json.JsonSerializer.NonGeneric.SerializeAsync(ms, 100);
27 | ms.Position = 0;
28 | var i = await Utf8Json.JsonSerializer.NonGeneric.DeserializeAsync(typeof(int), ms);
29 | ((int)i).Is(100);
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/sandbox/ConsoleAppNetCore/ConsoleAppNetCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 | Debug;Release;DumpAsm
7 |
8 |
9 |
10 | true
11 |
12 |
13 |
14 | true
15 |
16 |
17 |
18 | true
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/src/Utf8Json/Attributes.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace Utf8Json
6 | {
7 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
8 | public class JsonFormatterAttribute : Attribute
9 | {
10 | public Type FormatterType { get; private set; }
11 | public object[] Arguments { get; private set; }
12 |
13 | public JsonFormatterAttribute(Type formatterType)
14 | {
15 | this.FormatterType = formatterType;
16 | }
17 |
18 | public JsonFormatterAttribute(Type formatterType, params object[] arguments)
19 | {
20 | this.FormatterType = formatterType;
21 | this.Arguments = arguments;
22 | }
23 | }
24 |
25 | [AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = true)]
26 | public class SerializationConstructorAttribute : Attribute
27 | {
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/BassClassDeserializeTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public abstract class Base1
9 | {
10 | public int B1 { get { return _b1; } }
11 | private int _b1 = -1;
12 | }
13 |
14 | public abstract class Base2 : Base1
15 | {
16 | public int B2 { get { return _b2; } }
17 | private int _b2 = -2;
18 | }
19 |
20 | public class Impl1_2 : Base2
21 | {
22 | public string Name { get { return name; } }
23 | private string name = "none";
24 | }
25 |
26 | public class BassClassDeserializeTest
27 | {
28 | [Fact]
29 | public void Deserialize()
30 | {
31 | var json = "{\"_b1\":10,\"_b2\":99,\"name\":\"foobar\"}";
32 |
33 | var r = JsonSerializer.Deserialize(json, Utf8Json.Resolvers.StandardResolver.AllowPrivate);
34 |
35 | r.B1.Is(10);
36 | r.B2.Is(99);
37 | r.Name.Is("foobar");
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/WithBomTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Linq;
5 | using Xunit;
6 |
7 | namespace Utf8Json.Tests
8 | {
9 | public class WithBomTest
10 | {
11 |
12 |
13 | public class MyClass
14 | {
15 | public int MyProperty { get; set; }
16 | }
17 |
18 | [Fact]
19 | public void Bom()
20 | {
21 | var bin = JsonSerializer.Serialize(new MyClass { MyProperty = 100 });
22 | var withBom = Encoding.UTF8.GetPreamble().Concat(bin).ToArray();
23 | var hugahuga = JsonSerializer.Deserialize(withBom);
24 |
25 | hugahuga.MyProperty.Is(100);
26 |
27 | JsonSerializer.Deserialize(JsonSerializer.Serialize(1)).Is(1);
28 | JsonSerializer.Deserialize(JsonSerializer.Serialize(12)).Is(12);
29 | JsonSerializer.Deserialize(JsonSerializer.Serialize(123)).Is(123);
30 | JsonSerializer.Deserialize(JsonSerializer.Serialize(1234)).Is(1234);
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Utf8Json.UnityClient.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 11.00
3 | # Visual Studio 2010
4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utf8Json.UnityClient", "Assembly-CSharp.csproj", "{E5999E68-5892-2490-5531-6EE78A7A30D9}"
5 | EndProject
6 | Global
7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
8 | Debug|Any CPU = Debug|Any CPU
9 | Release|Any CPU = Release|Any CPU
10 | EndGlobalSection
11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
12 | {E5999E68-5892-2490-5531-6EE78A7A30D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13 | {E5999E68-5892-2490-5531-6EE78A7A30D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
14 | {E5999E68-5892-2490-5531-6EE78A7A30D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
15 | {E5999E68-5892-2490-5531-6EE78A7A30D9}.Release|Any CPU.Build.0 = Release|Any CPU
16 | EndGlobalSection
17 | GlobalSection(SolutionProperties) = preSolution
18 | HideSolutionNode = FALSE
19 | EndGlobalSection
20 | GlobalSection(MonoDevelopProperties) = preSolution
21 | StartupItem = Assembly-CSharp.csproj
22 | EndGlobalSection
23 | EndGlobal
24 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Yoshifumi Kawai
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/DecimalTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class DecimalTest
9 | {
10 | public class Foo
11 | {
12 | public decimal Bar { get; set; }
13 | public string More { get; set; }
14 | }
15 |
16 | [Fact]
17 | public void D()
18 | {
19 | var d = decimal.MaxValue;
20 | var bin = JsonSerializer.Serialize(d);
21 | JsonSerializer.Deserialize(bin).Is(d);
22 |
23 | JsonSerializer.Deserialize("79228162514264337593543950335.0").Is(decimal.MaxValue);
24 | JsonSerializer.Deserialize("\"79228162514264337593543950335.0\"").Is(decimal.MaxValue);
25 |
26 | var foo = JsonSerializer.Serialize(new Foo { Bar = -31.42323m, More = "mmmm" });
27 | var ddd = JsonSerializer.Deserialize(foo);
28 | ddd.Bar.Is(-31.42323m);
29 | ddd.More.Is("mmmm");
30 |
31 | JsonSerializer.Deserialize("{\"Bar\":1.23}").Bar.Is(1.23m);
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Utf8Json/Formatters/AnonymousFormatter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Utf8Json.Formatters
4 | {
5 | public sealed class AnonymousFormatter : IJsonFormatter
6 | {
7 | readonly JsonSerializeAction serialize;
8 | readonly JsonDeserializeFunc deserialize;
9 |
10 | public AnonymousFormatter(JsonSerializeAction serialize, JsonDeserializeFunc deserialize)
11 | {
12 | this.serialize = serialize;
13 | this.deserialize = deserialize;
14 | }
15 |
16 | public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
17 | {
18 | if (serialize == null) throw new InvalidOperationException(this.GetType().Name + " does not support Serialize.");
19 | serialize(ref writer, value, formatterResolver);
20 | }
21 |
22 | public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
23 | {
24 | if (deserialize == null) throw new InvalidOperationException(this.GetType().Name + " does not support Deserialize.");
25 | return deserialize(ref reader, formatterResolver);
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/sandbox/ConsoleAppNet461/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // アセンブリに関する一般情報は以下の属性セットをとおして制御されます。
6 | // アセンブリに関連付けられている情報を変更するには、
7 | // これらの属性値を変更してください。
8 | [assembly: AssemblyTitle("ConsoleAppNet461")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("ConsoleAppNet461")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // ComVisible を false に設定すると、このアセンブリ内の型は COM コンポーネントから
18 | // 参照できなくなります。COM からこのアセンブリ内の型にアクセスする必要がある場合は、
19 | // その型の ComVisible 属性を true に設定してください。
20 | [assembly: ComVisible(false)]
21 |
22 | // このプロジェクトが COM に公開される場合、次の GUID が typelib の ID になります
23 | [assembly: Guid("b98f5a74-0a8b-4869-9837-91fe8a4703e9")]
24 |
25 | // アセンブリのバージョン情報は次の 4 つの値で構成されています:
26 | //
27 | // メジャー バージョン
28 | // マイナー バージョン
29 | // ビルド番号
30 | // Revision
31 | //
32 | // すべての値を指定するか、次を使用してビルド番号とリビジョン番号を既定に設定できます
33 | // 既定値にすることができます:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/sandbox/DynamicCodeDumper/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // アセンブリに関する一般情報は以下の属性セットをとおして制御されます。
6 | // アセンブリに関連付けられている情報を変更するには、
7 | // これらの属性値を変更してください。
8 | [assembly: AssemblyTitle("DynamicCodeDumper")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("DynamicCodeDumper")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // ComVisible を false に設定すると、このアセンブリ内の型は COM コンポーネントから
18 | // 参照できなくなります。COM からこのアセンブリ内の型にアクセスする必要がある場合は、
19 | // その型の ComVisible 属性を true に設定してください。
20 | [assembly: ComVisible(false)]
21 |
22 | // このプロジェクトが COM に公開される場合、次の GUID が typelib の ID になります
23 | [assembly: Guid("7311c780-e172-45ce-bf8c-81c9dffeb5f5")]
24 |
25 | // アセンブリのバージョン情報は次の 4 つの値で構成されています:
26 | //
27 | // メジャー バージョン
28 | // マイナー バージョン
29 | // ビルド番号
30 | // Revision
31 | //
32 | // すべての値を指定するか、次を使用してビルド番号とリビジョン番号を既定に設定できます
33 | // 既定値にすることができます:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/src/Utf8Json.CodeGenerator/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // アセンブリに関する一般情報は以下の属性セットをとおして制御されます。
6 | // アセンブリに関連付けられている情報を変更するには、
7 | // これらの属性値を変更してください。
8 | [assembly: AssemblyTitle("Utf8Json.CodeGenerator")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("Utf8Json.CodeGenerator")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // ComVisible を false に設定すると、このアセンブリ内の型は COM コンポーネントから
18 | // 参照できなくなります。COM からこのアセンブリ内の型にアクセスする必要がある場合は、
19 | // その型の ComVisible 属性を true に設定してください。
20 | [assembly: ComVisible(false)]
21 |
22 | // このプロジェクトが COM に公開される場合、次の GUID が typelib の ID になります
23 | [assembly: Guid("32b23162-7c3e-458b-a721-392b453ff1c5")]
24 |
25 | // アセンブリのバージョン情報は次の 4 つの値で構成されています:
26 | //
27 | // メジャー バージョン
28 | // マイナー バージョン
29 | // ビルド番号
30 | // Revision
31 | //
32 | // すべての値を指定するか、次を使用してビルド番号とリビジョン番号を既定に設定できます
33 | // 既定値にすることができます:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/Utf8Json.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp2.0
5 |
6 | false
7 |
8 | Debug;Release;DumpAsm
9 |
10 |
11 |
12 | true
13 | true
14 |
15 |
16 |
17 | true
18 | true
19 |
20 |
21 |
22 | true
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/src/Utf8Json/IJsonFormatter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Utf8Json
4 | {
5 | public delegate void JsonSerializeAction(ref JsonWriter writer, T value, IJsonFormatterResolver resolver);
6 | public delegate T JsonDeserializeFunc(ref JsonReader reader, IJsonFormatterResolver resolver);
7 |
8 | public interface IJsonFormatter
9 | {
10 | }
11 |
12 | public interface IJsonFormatter : IJsonFormatter
13 | {
14 | void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver);
15 | T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver);
16 | }
17 |
18 | public interface IObjectPropertyNameFormatter
19 | {
20 | void SerializeToPropertyName(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver);
21 | T DeserializeFromPropertyName(ref JsonReader reader, IJsonFormatterResolver formatterResolver);
22 | }
23 |
24 | public static class JsonFormatterExtensions
25 | {
26 | public static string ToJsonString(this IJsonFormatter formatter, T value, IJsonFormatterResolver formatterResolver)
27 | {
28 | var writer = new JsonWriter();
29 | formatter.Serialize(ref writer, value, formatterResolver);
30 | return writer.ToString();
31 | }
32 | }
33 | }
--------------------------------------------------------------------------------
/sandbox/PerfBenchmark/Program.cs:
--------------------------------------------------------------------------------
1 | using BenchmarkDotNet.Attributes;
2 | using BenchmarkDotNet.Configs;
3 | using BenchmarkDotNet.Diagnosers;
4 | using BenchmarkDotNet.Environments;
5 | using BenchmarkDotNet.Exporters;
6 | using BenchmarkDotNet.Jobs;
7 | using BenchmarkDotNet.Running;
8 | using BenchmarkDotNet.Toolchains.CsProj;
9 | using PerfBenchmark;
10 |
11 | class Program
12 | {
13 | static void Main(string[] args)
14 | {
15 | var switcher = new BenchmarkSwitcher(new[]
16 | {
17 | typeof(SerializeBenchmark),
18 | typeof(DeserializeBenchmark),
19 | typeof(JsonSerializeBench)
20 | });
21 |
22 | // args = new string[] { "0" };
23 |
24 | #if DEBUG
25 | var b = new DeserializeBenchmark();
26 | b.Utf8JsonSerializer();
27 | #else
28 |
29 |
30 | switcher.Run(args);
31 | #endif
32 | }
33 | }
34 |
35 | public class BenchmarkConfig : ManualConfig
36 | {
37 | public BenchmarkConfig()
38 | {
39 | Add(MarkdownExporter.GitHub);
40 | Add(MemoryDiagnoser.Default);
41 |
42 | var baseConfig = Job.ShortRun.WithLaunchCount(1).WithTargetCount(1).WithWarmupCount(1);
43 | Add(baseConfig.With(Runtime.Clr).With(Jit.RyuJit).With(Platform.X64));
44 | //Add(baseConfig.With(Runtime.Core).With(Jit.RyuJit).With(CsProjCoreToolchain.NetCoreApp20));
45 | }
46 | }
--------------------------------------------------------------------------------
/sandbox/PerfBenchmark/PerfBenchmark.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0;net47
6 | Debug;Release;DumpAsm
7 |
8 |
9 |
10 | true
11 |
12 |
13 |
14 | true
15 |
16 |
17 |
18 | true
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/make_unity_symlink.bat:
--------------------------------------------------------------------------------
1 | cd /d %~dp0
2 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\Attributes.cs" "..\..\..\..\Utf8Json\Attributes.cs"
3 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\IJsonFormatter.cs" "..\..\..\..\Utf8Json\IJsonFormatter.cs"
4 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\IJsonFormatterResolver.cs" "..\..\..\..\Utf8Json\IJsonFormatterResolver.cs"
5 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\JsonReader.cs" "..\..\..\..\Utf8Json\JsonReader.cs"
6 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\JsonWriter.cs" "..\..\..\..\Utf8Json\JsonWriter.cs"
7 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\JsonSerializer.cs" "..\..\..\..\Utf8Json\JsonSerializer.cs"
8 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\JsonSerializer.NonGeneric.cs" "..\..\..\..\Utf8Json\JsonSerializer.NonGeneric.cs"
9 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\JsonToken.cs" "..\..\..\..\Utf8Json\JsonToken.cs"
10 | mklink /D ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\Resolvers" "..\..\..\..\Utf8Json\Resolvers"
11 | mklink /D ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\Formatters" "..\..\..\..\Utf8Json\Formatters"
12 | mklink /D ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\Internal" "..\..\..\..\Utf8Json\Internal"
13 | mklink ".\src\Utf8Json.UnityClient\Assets\Scripts\Utf8Json\Unity\ResolverAndFormatters.cs" "..\..\..\..\..\Utf8Json.UnityShims\ResolverAndFormatters.cs"
--------------------------------------------------------------------------------
/src/Utf8Json.UnityShims/Utf8Json.UnityShims.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0;NET45;NET47
5 | Debug;Release
6 | true
7 | opensource.snk
8 | false
9 |
10 |
11 |
12 | 4
13 | true
14 | TRACE;DEBUG;NETSTANDARD;NETSTANDARD2_0;NETSTANDARD2_0
15 | true
16 |
17 |
18 |
19 | 4
20 | true
21 | TRACE;RELEASE;NETSTANDARD;NETSTANDARD2_0
22 | true
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/CommentTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class CommentTest
9 | {
10 | public class MyClass
11 | {
12 | public int Foo { get; set; }
13 | public bool Bar { get; set; }
14 | public double Baz { get; set; }
15 | }
16 |
17 | [Fact]
18 | public void SingleLine()
19 | {
20 | var x = JsonSerializer.Deserialize(@"
21 | {
22 | // foo bar baz
23 | ""Foo"":100,
24 | // bar
25 | ""Bar"":true
26 | }");
27 |
28 | x.Foo.Is(100);
29 | x.Bar.Is(true);
30 | }
31 |
32 | [Fact]
33 | public void MultiLine()
34 | {
35 | var x = JsonSerializer.Deserialize(@"
36 | {
37 | /*
38 | yarou
39 | yarou
40 | yaruo
41 | */
42 | ""Foo"":100,
43 | /* oooooo */
44 | ""Bar"":true
45 | }");
46 |
47 | x.Foo.Is(100);
48 | x.Bar.Is(true);
49 | }
50 |
51 | [Fact]
52 | public void LastLine()
53 | {
54 | var x = JsonSerializer.Deserialize(@"
55 | {
56 | ""Foo"":100, // this is foo
57 | ""Bar"":true, /* this is bar */
58 | ""Baz"":10.9
59 | }");
60 |
61 | x.Foo.Is(100);
62 | x.Bar.Is(true);
63 | x.Baz.Is(10.9);
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/Assets/Scripts/Tests/_Loader.cs:
--------------------------------------------------------------------------------
1 | using RuntimeUnitTestToolkit;
2 | using UnityEngine;
3 | using System.Collections;
4 | using System;
5 | using System.IO;
6 | using System.Text;
7 |
8 | namespace Utf8Json.UnityClient.Tests
9 | {
10 | public static class UnitTestLoader
11 | {
12 | [UnityEngine.RuntimeInitializeOnLoadMethod(UnityEngine.RuntimeInitializeLoadType.BeforeSceneLoad)]
13 | public static void Register()
14 | {
15 | UnitTest.RegisterAllMethods();
16 | UnitTest.RegisterAllMethods();
17 |
18 | //#if !UNITY_METRO
19 |
20 | // // Register Tests
21 | // UnitTest.RegisterAllMethods();
22 | // UnitTest.RegisterAllMethods();
23 | // UnitTest.RegisterAllMethods();
24 | // UnitTest.RegisterAllMethods();
25 | // UnitTest.RegisterAllMethods();
26 | // UnitTest.RegisterAllMethods();
27 | // UnitTest.RegisterAllMethods();
28 | //#if ENABLE_UNSAFE_MSGPACK
29 | // UnitTest.RegisterAllMethods();
30 | //#endif
31 |
32 | // UnitTest.RegisterAllMethods();
33 |
34 | // UnitTest.RegisterAllMethods();
35 |
36 | //#endif
37 | }
38 | }
39 | }
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/Physics2DSettings.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!19 &1
4 | Physics2DSettings:
5 | m_ObjectHideFlags: 0
6 | serializedVersion: 3
7 | m_Gravity: {x: 0, y: -9.81}
8 | m_DefaultMaterial: {fileID: 0}
9 | m_VelocityIterations: 8
10 | m_PositionIterations: 3
11 | m_VelocityThreshold: 1
12 | m_MaxLinearCorrection: 0.2
13 | m_MaxAngularCorrection: 8
14 | m_MaxTranslationSpeed: 100
15 | m_MaxRotationSpeed: 360
16 | m_BaumgarteScale: 0.2
17 | m_BaumgarteTimeOfImpactScale: 0.75
18 | m_TimeToSleep: 0.5
19 | m_LinearSleepTolerance: 0.01
20 | m_AngularSleepTolerance: 2
21 | m_DefaultContactOffset: 0.01
22 | m_AutoSimulation: 1
23 | m_QueriesHitTriggers: 1
24 | m_QueriesStartInColliders: 1
25 | m_ChangeStopsCallbacks: 0
26 | m_CallbacksOnDisable: 1
27 | m_AutoSyncTransforms: 1
28 | m_AlwaysShowColliders: 0
29 | m_ShowColliderSleep: 1
30 | m_ShowColliderContacts: 0
31 | m_ShowColliderAABB: 0
32 | m_ContactArrowScale: 0.2
33 | m_ColliderAwakeColor: {r: 0.5686275, g: 0.95686275, b: 0.54509807, a: 0.7529412}
34 | m_ColliderAsleepColor: {r: 0.5686275, g: 0.95686275, b: 0.54509807, a: 0.36078432}
35 | m_ColliderContactColor: {r: 1, g: 0, b: 1, a: 0.6862745}
36 | m_ColliderAABBColor: {r: 1, g: 1, b: 0, a: 0.2509804}
37 | m_LayerCollisionMatrix: ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
38 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/GenericCustomFormatterTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class GenericCustomFormatterTest
9 | {
10 | [JsonFormatter(typeof(CustomFormatter<>))]
11 | public class Custom
12 | {
13 | public T MyProperty { get; set; }
14 | }
15 |
16 | public class Custom2
17 | {
18 | [JsonFormatter(typeof(CustomFormatter<>))]
19 | public T MyProperty { get; set; }
20 | }
21 |
22 |
23 | public class CustomFormatter : IJsonFormatter
24 | {
25 | public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
26 | {
27 | throw new NotImplementedException();
28 | }
29 |
30 | public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
31 | {
32 | // throws NotImplementedException for OK.
33 | throw new NotImplementedException();
34 | }
35 | }
36 |
37 | [Fact]
38 | public void Foo()
39 | {
40 | AssertEx.Throws(() =>
41 | {
42 | JsonSerializer.Serialize(new Custom() { MyProperty = 9999 });
43 | });
44 |
45 | // TODO:Currently not supported...
46 | // AssertEx.Throws(() =>
47 | //{
48 | // JsonSerializer.Serialize(new Custom2() { MyProperty = 9999 });
49 | //}
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/NavMeshAreas.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!126 &1
4 | NavMeshProjectSettings:
5 | m_ObjectHideFlags: 0
6 | serializedVersion: 2
7 | areas:
8 | - name: Walkable
9 | cost: 1
10 | - name: Not Walkable
11 | cost: 1
12 | - name: Jump
13 | cost: 2
14 | - name:
15 | cost: 1
16 | - name:
17 | cost: 1
18 | - name:
19 | cost: 1
20 | - name:
21 | cost: 1
22 | - name:
23 | cost: 1
24 | - name:
25 | cost: 1
26 | - name:
27 | cost: 1
28 | - name:
29 | cost: 1
30 | - name:
31 | cost: 1
32 | - name:
33 | cost: 1
34 | - name:
35 | cost: 1
36 | - name:
37 | cost: 1
38 | - name:
39 | cost: 1
40 | - name:
41 | cost: 1
42 | - name:
43 | cost: 1
44 | - name:
45 | cost: 1
46 | - name:
47 | cost: 1
48 | - name:
49 | cost: 1
50 | - name:
51 | cost: 1
52 | - name:
53 | cost: 1
54 | - name:
55 | cost: 1
56 | - name:
57 | cost: 1
58 | - name:
59 | cost: 1
60 | - name:
61 | cost: 1
62 | - name:
63 | cost: 1
64 | - name:
65 | cost: 1
66 | - name:
67 | cost: 1
68 | - name:
69 | cost: 1
70 | - name:
71 | cost: 1
72 | m_LastAgentTypeID: -887442657
73 | m_Settings:
74 | - serializedVersion: 2
75 | agentTypeID: 0
76 | agentRadius: 0.5
77 | agentHeight: 2
78 | agentSlope: 45
79 | agentClimb: 0.75
80 | ledgeDropHeight: 0
81 | maxJumpAcrossDistance: 0
82 | minRegionArea: 2
83 | manualCellSize: 0
84 | cellSize: 0.16666667
85 | manualTileSize: 0
86 | tileSize: 256
87 | accuratePlacement: 0
88 | m_SettingNames:
89 | - Humanoid
90 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/CompositeResolverTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Utf8Json.Formatters;
5 | using Utf8Json.Resolvers;
6 | using Xunit;
7 |
8 | namespace Utf8Json.Tests
9 | {
10 | public class CompositeResolverTest
11 | {
12 | class TestFormatter : IJsonFormatter
13 | {
14 | public MyClass Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
15 | {
16 | var i = reader.ReadInt32();
17 | return new MyClass { MyProperty = i * 2 };
18 | }
19 |
20 | public void Serialize(ref JsonWriter writer, MyClass value, IJsonFormatterResolver formatterResolver)
21 | {
22 | writer.WriteInt32(value.MyProperty * 2);
23 | }
24 | }
25 |
26 | public class MyClass
27 | {
28 | public int MyProperty { get; set; }
29 | }
30 |
31 | [Fact]
32 | public void Composite()
33 | {
34 | var emptyResolver = CompositeResolver.Create(new IJsonFormatter[0], new IJsonFormatterResolver[0]);
35 | AssertEx.Catch(() => emptyResolver.GetFormatterWithVerify());
36 |
37 | var resolver = CompositeResolver.Create(new[] { new TestFormatter() }, new[] { BuiltinResolver.Instance });
38 | resolver.GetFormatterWithVerify().IsInstanceOf();
39 |
40 | var json = JsonSerializer.ToJsonString(new MyClass { MyProperty = 3 }, resolver);
41 | json.Is("6");
42 | JsonSerializer.Deserialize(json, resolver).MyProperty.Is(12);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/docs/Utf8Json.AspNetCoreMvcFormatter.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Utf8Json.AspNetCoreMvcFormatter
5 | 1.3.7
6 | Utf8Json AspNetCoreMvcFormatter
7 | neuecc
8 | neuecc
9 | https://github.com/neuecc/Utf8Json/
10 | false
11 | Utf8Json ASP.NET Core Mvc Input/Output Formatter.
12 | Initial Release.
13 | Json, Serialization, Formatter, Serializer, Unity, Xamarin, ASPNET
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/SkipReadTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class SkipReadTest
9 | {
10 | static JsonReader CreateReader(string json)
11 | {
12 | return new JsonReader(Encoding.UTF8.GetBytes(json));
13 | }
14 |
15 | [Fact]
16 | public void Skip()
17 | {
18 | CreateReader(" true").ReadBoolean().IsTrue();
19 | CreateReader(" 1").ReadByte().Is((byte)1);
20 | CreateReader(" 1.21341").ReadDouble().Is(1.21341);
21 | CreateReader(" 2").ReadInt16().Is((short)2);
22 | CreateReader(" 2").ReadInt32().Is((int)2);
23 | CreateReader(" 2").ReadInt64().Is((long)2);
24 | CreateReader(" [").ReadIsBeginArray().IsTrue();
25 | CreateReader(" {").ReadIsBeginObject().IsTrue();
26 | CreateReader(" ]").ReadIsEndArray().IsTrue();
27 | CreateReader(" }").ReadIsEndObject().IsTrue();
28 | CreateReader(" :").ReadIsNameSeparator().IsTrue();
29 | CreateReader(" null").ReadIsNull().IsTrue();
30 | CreateReader(" ,").ReadIsValueSeparator().IsTrue();
31 | CreateReader(" 3").ReadSByte().Is((sbyte)3);
32 | CreateReader(" 1.4").ReadSingle().Is((float)1.4);
33 | CreateReader(" \"foo\"").ReadString().Is("foo");
34 | CreateReader(" 1").ReadUInt16().Is((UInt16)1);
35 | CreateReader(" 1").ReadUInt32().Is((UInt32)1);
36 | CreateReader(" 1").ReadUInt64().Is((UInt64)1);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Utf8Json/IJsonFormatterResolver.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 |
4 | namespace Utf8Json
5 | {
6 | public interface IJsonFormatterResolver
7 | {
8 | IJsonFormatter GetFormatter();
9 | }
10 |
11 | public static class JsonFormatterResolverExtensions
12 | {
13 | public static IJsonFormatter GetFormatterWithVerify(this IJsonFormatterResolver resolver)
14 | {
15 | IJsonFormatter formatter;
16 | try
17 | {
18 | formatter = resolver.GetFormatter();
19 | }
20 | catch (TypeInitializationException ex)
21 | {
22 | Exception inner = ex;
23 | while (inner.InnerException != null)
24 | {
25 | inner = inner.InnerException;
26 | }
27 |
28 | throw inner;
29 | }
30 |
31 | if (formatter == null)
32 | {
33 | throw new FormatterNotRegisteredException(typeof(T).FullName + " is not registered in this resolver. resolver:" + resolver.GetType().Name);
34 | }
35 |
36 | return formatter;
37 | }
38 |
39 | public static object GetFormatterDynamic(this IJsonFormatterResolver resolver, Type type)
40 | {
41 | var methodInfo = typeof(IJsonFormatterResolver).GetRuntimeMethod("GetFormatter", Type.EmptyTypes);
42 |
43 | var formatter = methodInfo.MakeGenericMethod(type).Invoke(resolver, null);
44 | return formatter;
45 | }
46 | }
47 |
48 | public class FormatterNotRegisteredException : Exception
49 | {
50 | public FormatterNotRegisteredException(string message) : base(message)
51 | {
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/ArrayPool.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Utf8Json.Internal
4 | {
5 | internal sealed class BufferPool : ArrayPool
6 | {
7 | public static readonly BufferPool Default = new BufferPool(65535);
8 |
9 | public BufferPool(int bufferLength)
10 | : base(bufferLength)
11 | {
12 | }
13 | }
14 |
15 | internal class ArrayPool
16 | {
17 | readonly int bufferLength;
18 | readonly object gate;
19 | int index;
20 | T[][] buffers;
21 |
22 | public ArrayPool(int bufferLength)
23 | {
24 | this.bufferLength = bufferLength;
25 | this.buffers = new T[4][];
26 | this.gate = new object();
27 | }
28 |
29 | public T[] Rent()
30 | {
31 | lock (gate)
32 | {
33 | if (index >= buffers.Length)
34 | {
35 | Array.Resize(ref buffers, buffers.Length * 2);
36 | }
37 |
38 | if (buffers[index] == null)
39 | {
40 | buffers[index] = new T[bufferLength];
41 | }
42 |
43 | var buffer = buffers[index];
44 | buffers[index] = null;
45 | index++;
46 |
47 | return buffer;
48 | }
49 | }
50 |
51 | public void Return(T[] array)
52 | {
53 | if (array.Length != bufferLength)
54 | {
55 | throw new InvalidOperationException("return buffer is not from pool");
56 | }
57 |
58 | lock (gate)
59 | {
60 | if (index != 0)
61 | {
62 | buffers[--index] = array;
63 | }
64 | }
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/ValueTupleTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class ValueTupleTest
9 | {
10 | T Convert(T value)
11 | {
12 | return JsonSerializer.Deserialize(JsonSerializer.Serialize(value));
13 | }
14 |
15 | public static object valueTupleData = new object[]
16 | {
17 | new object[] { (1, 2) },
18 | new object[] { (1, 2, 3) },
19 | new object[] { (1, 2, 3, 4) },
20 | new object[] { (1, 2, 3, 4, 5) },
21 | new object[] { (1, 2, 3, 4, 5,6) },
22 | new object[] { (1, 2, 3, 4, 5,6,7) },
23 | new object[] { (1, 2, 3, 4, 5,6,7,8) },
24 | new object[] { (1, 2, 3, 4, 5,6,7,8,9) },
25 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10) },
26 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11) },
27 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12) },
28 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13) },
29 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14) },
30 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15) },
31 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16) },
32 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17) },
33 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17,18) },
34 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19) },
35 | new object[] { (1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) },
36 | };
37 |
38 | [Theory]
39 | [MemberData(nameof(valueTupleData))]
40 | public void ValueTuple(T x)
41 | {
42 | Convert(x).Is(x);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/ExcludeNullTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class ExcludeNullTest
9 | {
10 | [Fact]
11 | public void Foo()
12 | {
13 | var answer1 = "{\"Age\":0,\"Name\":null}";
14 | var answer2 = "{\"Age\":0}";
15 | JsonSerializer.ToJsonString(new SimplePerson(), Utf8Json.Resolvers.StandardResolver.Default).Is(answer1);
16 | JsonSerializer.ToJsonString(new SimplePerson(), Utf8Json.Resolvers.StandardResolver.ExcludeNull).Is(answer2);
17 | JsonSerializer.ToJsonString(new SimplePerson(), Utf8Json.Resolvers.StandardResolver.AllowPrivate).Is(answer1);
18 | JsonSerializer.ToJsonString(new SimplePerson(), Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNull).Is(answer2);
19 |
20 | var answer3 = "{\"Age\":null,\"Name\":null}";
21 | var answer4 = "{}";
22 | JsonSerializer.ToJsonString(new IncludeNullablePerson(), Utf8Json.Resolvers.StandardResolver.Default).Is(answer3);
23 | JsonSerializer.ToJsonString(new IncludeNullablePerson(), Utf8Json.Resolvers.StandardResolver.ExcludeNull).Is(answer4);
24 | JsonSerializer.ToJsonString(new IncludeNullablePerson(), Utf8Json.Resolvers.StandardResolver.AllowPrivate).Is(answer3);
25 | JsonSerializer.ToJsonString(new IncludeNullablePerson(), Utf8Json.Resolvers.StandardResolver.AllowPrivateExcludeNull).Is(answer4);
26 | }
27 |
28 | public class SimplePerson
29 | {
30 | public int Age { get; set; }
31 | public string Name { get; set; }
32 | }
33 |
34 | public class IncludeNullablePerson
35 | {
36 | public int? Age { get; set; }
37 | public string Name { get; set; }
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/docs/Utf8Json.UnityShims.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Utf8Json.UnityShims
5 | 1.3.7
6 | Utf8Json UnityShims
7 | neuecc
8 | neuecc
9 | https://github.com/neuecc/Utf8Json/
10 | false
11 | Unity shims and formatters for Utf8Json.
12 | Json, Serialization, Formatter, Serializer, Unity, Xamarin
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/DynamicObjectFallback.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Text;
5 | using Xunit;
6 |
7 | namespace Utf8Json.Tests
8 | {
9 | public class DynamicObjectFallbackTestContainer
10 | {
11 | public int MyProperty { get; set; }
12 |
13 | public object MoreObject { get; set; }
14 | }
15 |
16 |
17 | public class DynamicObjectFallbackTest
18 | {
19 | [Fact]
20 | public void DynamicObject()
21 | {
22 | var testData = new object[]
23 | {
24 | new DynamicObjectFallbackTestContainer
25 | {
26 | MyProperty = 100,
27 | MoreObject = new string[]{"a", "b", "c" }
28 | },
29 |
30 | new DynamicObjectFallbackTestContainer
31 | {
32 | MyProperty = 300,
33 | MoreObject = new SharedData.SimlpeStringKeyData
34 | {
35 | Prop1 = 10,
36 | Prop2 = SharedData.ByteEnum.C,
37 | Prop3 = 99999
38 | }
39 | },
40 | };
41 |
42 | var resolver = new MyResolver();
43 | var data1 = JsonSerializer.Serialize(testData, resolver);
44 |
45 | var json = Encoding.UTF8.GetString(data1);
46 |
47 | var reference = JsonConvert.SerializeObject(testData);
48 |
49 | json.Is(reference);
50 |
51 | }
52 | }
53 |
54 | class MyResolver : IJsonFormatterResolver
55 | {
56 | public IJsonFormatter GetFormatter()
57 | {
58 | if (typeof(T).IsEnum)
59 | {
60 | return Resolvers.EnumResolver.UnderlyingValue.GetFormatter();
61 | }
62 | else
63 | {
64 | return Resolvers.StandardResolver.Default.GetFormatter();
65 | }
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/StringMutator.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 |
4 | namespace Utf8Json.Internal
5 | {
6 | internal static class StringMutator
7 | {
8 | ///
9 | /// MyProperty -> MyProperty
10 | ///
11 | public static string Original(string s)
12 | {
13 | return s;
14 | }
15 |
16 | ///
17 | /// MyProperty -> myProperty
18 | ///
19 | public static string ToCamelCase(string s)
20 | {
21 | if (string.IsNullOrEmpty(s) || char.IsLower(s, 0))
22 | {
23 | return s;
24 | }
25 |
26 | var array = s.ToCharArray();
27 | array[0] = char.ToLowerInvariant(array[0]);
28 | return new string(array);
29 | }
30 |
31 | ///
32 | /// MyProperty -> my_property
33 | ///
34 | public static string ToSnakeCase(string s)
35 | {
36 | if (string.IsNullOrEmpty(s)) return s;
37 |
38 | var sb = new StringBuilder();
39 | for (int i = 0; i < s.Length; i++)
40 | {
41 | var c = s[i];
42 |
43 | if (Char.IsUpper(c))
44 | {
45 | // first
46 | if (i == 0)
47 | {
48 | sb.Append(char.ToLowerInvariant(c));
49 | }
50 | else if (char.IsUpper(s[i - 1])) // WriteIO => write_io
51 | {
52 | sb.Append(char.ToLowerInvariant(c));
53 | }
54 | else
55 | {
56 | sb.Append("_");
57 | sb.Append(char.ToLowerInvariant(c));
58 | }
59 | }
60 | else
61 | {
62 | sb.Append(c);
63 | }
64 | }
65 |
66 | return sb.ToString();
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/docs/Utf8Json.ImmutableCollection.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Utf8Json.ImmutableCollection
5 | 1.3.7
6 | Utf8Json Extension Resolver for ImmutableCollection
7 | neuecc
8 | neuecc
9 | https://github.com/neuecc/Utf8Json/
10 | false
11 | Add ImmutableCollection formatters to Utf8Json.
12 | Json, Serialization, Formatter, Serializer, Unity, Xamarin
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/UnsafeMemory.tt:
--------------------------------------------------------------------------------
1 | <#@ template debug="false" hostspecific="false" language="C#" #>
2 | <#@ assembly name="System.Core" #>
3 | <#@ import namespace="System.Linq" #>
4 | <#@ import namespace="System.Text" #>
5 | <#@ import namespace="System.Collections.Generic" #>
6 | <#@ output extension=".cs" #>
7 | <#
8 | var Max = 31;
9 | #>
10 | #if NETSTANDARD
11 |
12 | using System.Runtime.CompilerServices;
13 |
14 | namespace Utf8Json.Internal
15 | {
16 | public static partial class UnsafeMemory32
17 | {
18 | <# for(var i = 4; i <= Max; i++) { #>
19 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
20 | public static unsafe void WriteRaw<#= i #>(ref JsonWriter writer, byte[] src)
21 | {
22 | BinaryUtil.EnsureCapacity(ref writer.buffer, writer.offset, src.Length);
23 |
24 | fixed (byte* pSrc = &src[0])
25 | fixed (byte* pDst = &writer.buffer[writer.offset])
26 | {
27 | <# for(var j = 0; j < (i / 4); j++) { #>
28 | *(int*)(pDst + <#= (4 * j) #>) = *(int*)(pSrc + <#= (4 * j) #>);
29 | <# } #>
30 | <# if(i % 4 != 0) { #>
31 | *(int*)(pDst + <#= i - 4 #>) = *(int*)(pSrc + <#= (i - 4) #>);
32 | <# } #>
33 | }
34 |
35 | writer.offset += src.Length;
36 | }
37 |
38 | <# } #>
39 | }
40 |
41 | public static partial class UnsafeMemory64
42 | {
43 | <# for(var i = 8; i <= Max; i++) { #>
44 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
45 | public static unsafe void WriteRaw<#= i #>(ref JsonWriter writer, byte[] src)
46 | {
47 | BinaryUtil.EnsureCapacity(ref writer.buffer, writer.offset, src.Length);
48 |
49 | fixed (byte* pSrc = &src[0])
50 | fixed (byte* pDst = &writer.buffer[writer.offset])
51 | {
52 | <# for(var j = 0; j < (i / 8); j++) { #>
53 | *(long*)(pDst + <#= (8 * j) #>) = *(long*)(pSrc + <#= (8 * j) #>);
54 | <# } #>
55 | <# if(i % 8 != 0) { #>
56 | *(long*)(pDst + <#= i - 8 #>) = *(long*)(pSrc + <#= (i - 8) #>);
57 | <# } #>
58 | }
59 |
60 | writer.offset += src.Length;
61 | }
62 |
63 | <# } #>
64 | }
65 | }
66 |
67 | #endif
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/DataContractTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Linq;
7 | using System.Runtime.Serialization;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 | using Xunit;
11 |
12 | namespace Utf8Json.Tests
13 | {
14 |
15 |
16 |
17 | public class DataContractTest
18 | {
19 | [DataContract]
20 | public class MyClass
21 | {
22 | [DataMember(Order = 0)]
23 | public int MyProperty1 { get; set; }
24 | [DataMember(Order = 1)]
25 | public string MyProperty2;
26 | }
27 |
28 | [DataContract]
29 | public class MyClass1
30 | {
31 | [DataMember(Name = "mp1")]
32 | public int MyProperty1 { get; set; }
33 | [DataMember(Name = "mp2")]
34 | public string MyProperty2;
35 | }
36 |
37 | [DataContract]
38 | public class MyClass2
39 | {
40 | [DataMember]
41 | public int MyProperty1 { get; set; }
42 | [DataMember]
43 | public string MyProperty2;
44 | }
45 |
46 | [Fact]
47 | public void SerializeName()
48 | {
49 | var mc = new MyClass1 { MyProperty1 = 100, MyProperty2 = "foobar" };
50 |
51 | var bin = JsonSerializer.Serialize(mc);
52 |
53 | Encoding.UTF8.GetString(bin).Is(@"{""mp1"":100,""mp2"":""foobar""}");
54 |
55 | var mc2 = JsonSerializer.Deserialize(bin);
56 |
57 | mc.MyProperty1.Is(mc2.MyProperty1);
58 | mc.MyProperty2.Is(mc2.MyProperty2);
59 | }
60 |
61 | [Fact]
62 | public void Serialize()
63 | {
64 | var mc = new MyClass2 { MyProperty1 = 100, MyProperty2 = "foobar" };
65 |
66 | var bin = JsonSerializer.Serialize(mc);
67 | var mc2 = JsonSerializer.Deserialize(bin);
68 |
69 | mc.MyProperty1.Is(mc2.MyProperty1);
70 | mc.MyProperty2.Is(mc2.MyProperty2);
71 |
72 | Encoding.UTF8.GetString(bin).Is(@"{""MyProperty1"":100,""MyProperty2"":""foobar""}");
73 | }
74 | }
75 |
76 | }
77 |
--------------------------------------------------------------------------------
/src/Utf8Json/Resolvers/AttributeFormatterResolver.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 | using System.Collections.Generic;
4 | using System.Text;
5 | using Utf8Json.Internal;
6 |
7 | namespace Utf8Json.Resolvers
8 | {
9 | ///
10 | /// Get formatter from [JsonFormatter] attribute.
11 | ///
12 | public sealed class AttributeFormatterResolver : IJsonFormatterResolver
13 | {
14 | public static IJsonFormatterResolver Instance = new AttributeFormatterResolver();
15 |
16 | AttributeFormatterResolver()
17 | {
18 |
19 | }
20 |
21 | public IJsonFormatter GetFormatter()
22 | {
23 | return FormatterCache.formatter;
24 | }
25 |
26 | static class FormatterCache
27 | {
28 | public static readonly IJsonFormatter formatter;
29 |
30 | static FormatterCache()
31 | {
32 | #if (UNITY_METRO || UNITY_WSA) && !NETFX_CORE
33 | var attr = (JsonFormatterAttribute)typeof(T).GetCustomAttributes(typeof(JsonFormatterAttribute), true).FirstOrDefault();
34 | #else
35 | var attr = typeof(T).GetTypeInfo().GetCustomAttribute();
36 | #endif
37 | if (attr == null)
38 | {
39 | return;
40 | }
41 |
42 | try
43 | {
44 | if (attr.FormatterType.IsGenericType && !attr.FormatterType.GetTypeInfo().IsConstructedGenericType())
45 | {
46 | var t = attr.FormatterType.MakeGenericType(typeof(T)); // use T self
47 | formatter = (IJsonFormatter)Activator.CreateInstance(t, attr.Arguments);
48 | }
49 | else
50 | {
51 | formatter = (IJsonFormatter)Activator.CreateInstance(attr.FormatterType, attr.Arguments);
52 | }
53 | }
54 | catch (Exception ex)
55 | {
56 | throw new InvalidOperationException("Can not create formatter from JsonFormatterAttribute, check the target formatter is public and has constructor with right argument. FormatterType:" + attr.FormatterType.Name, ex);
57 | }
58 | }
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/ReadIsInTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Xunit;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class ReadIsInTest
9 | {
10 | static (int, int[]) Array(int[] xs)
11 | {
12 | var list = new List();
13 | var json = JsonSerializer.Serialize(xs);
14 | var reader = new JsonReader(json);
15 | var c = 0;
16 | while (reader.ReadIsInArray(ref c))
17 | {
18 | list.Add(reader.ReadInt32());
19 | }
20 | return (c, list.ToArray());
21 | }
22 |
23 | static (int, Dictionary) Object(object xs)
24 | {
25 | var list = new Dictionary();
26 | var json = JsonSerializer.Serialize(xs);
27 | var reader = new JsonReader(json);
28 | var c = 0;
29 | while (reader.ReadIsInObject(ref c))
30 | {
31 | var k = reader.ReadPropertyName();
32 | var v = reader.ReadInt32();
33 | list.Add(k, v);
34 | }
35 | return (c, list);
36 | }
37 |
38 | [Fact]
39 | public void IsInArrayTest()
40 | {
41 | var empty = Array(new int[0]);
42 | var one = Array(new int[] { 1 });
43 | var five = Array(new int[] { 1, 10, 100, 1000, 10000 });
44 |
45 | empty.Item1.Is(0); empty.Item2.Length.Is(0);
46 | one.Item1.Is(1); one.Item2.Is(1);
47 | five.Item1.Is(5); five.Item2.Is(1, 10, 100, 1000, 10000);
48 | }
49 |
50 | [Fact]
51 | public void IsInObjectTest()
52 | {
53 | var empty = Object(new { });
54 | var one = Object(new { OK = 1 });
55 | var five = Object(new { A = 1, B = 10, C = 100, D = 1000, E = 10000 });
56 |
57 | empty.Item1.Is(0); empty.Item2.Count.Is(0);
58 | one.Item1.Is(1); one.Item2.Count.Is(1);
59 | one.Item2["OK"].Is(1);
60 |
61 | five.Item1.Is(5); five.Item2.Count.Is(5);
62 | five.Item2["A"].Is(1);
63 | five.Item2["B"].Is(10);
64 | five.Item2["C"].Is(100);
65 | five.Item2["D"].Is(1000);
66 | five.Item2["E"].Is(10000);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/MultiDimentionalArrayTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class MultiDimentionalArrayTest
9 | {
10 | T Convert(T value)
11 | {
12 | return JsonSerializer.Deserialize(JsonSerializer.Serialize(value));
13 | }
14 |
15 | [Theory]
16 | [InlineData(100, 100, 10, 5)]
17 | [InlineData(10, 20, 15, 5)]
18 | [InlineData(3, 5, 10, 15)]
19 | [InlineData(3, 5, 10, 15)]
20 | public void MultiDimentional(int dataI, int dataJ, int dataK, int dataL)
21 | {
22 | var two = new ValueTuple[dataI, dataJ];
23 | var three = new ValueTuple[dataI, dataJ, dataK];
24 | var four = new ValueTuple[dataI, dataJ, dataK, dataL];
25 |
26 | for (int i = 0; i < dataI; i++)
27 | {
28 | for (int j = 0; j < dataJ; j++)
29 | {
30 | two[i, j] = (i, j);
31 | for (int k = 0; k < dataK; k++)
32 | {
33 | three[i, j, k] = (i, j, k);
34 | for (int l = 0; l < dataL; l++)
35 | {
36 | four[i, j, k, l] = (i, j, k, l);
37 | }
38 | }
39 | }
40 | }
41 |
42 | var cTwo = Convert(two);
43 | var cThree = Convert(three);
44 | var cFour = Convert(four);
45 |
46 | cTwo.Length.Is(two.Length);
47 | cThree.Length.Is(three.Length);
48 | cFour.Length.Is(four.Length);
49 |
50 | for (int i = 0; i < dataI; i++)
51 | {
52 | for (int j = 0; j < dataJ; j++)
53 | {
54 | cTwo[i, j].Is(two[i, j]);
55 | for (int k = 0; k < dataK; k++)
56 | {
57 | cThree[i, j, k].Is(three[i, j, k]);
58 | for (int l = 0; l < dataL; l++)
59 | {
60 | cFour[i, j, k, l].Is(four[i, j, k, l]);
61 | }
62 | }
63 | }
64 | }
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/DictionaryTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Concurrent;
3 | using System.Collections.Generic;
4 | using System.Collections.ObjectModel;
5 | using System.Linq;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 | using Xunit;
9 |
10 |
11 | namespace Utf8Json.Tests
12 | {
13 | public class DictionaryTest
14 | {
15 | T Convert(T value)
16 | {
17 | return JsonSerializer.Deserialize(JsonSerializer.Serialize(value));
18 | }
19 |
20 | public static object dictionaryTestData = new object[]
21 | {
22 | new object[]{ new Dictionary() { { 1, 100 } }, null },
23 | new object[]{ new ReadOnlyDictionary(new Dictionary() { { 1, 100 } }), null },
24 | new object[]{ new SortedList() { { 1, 100 } }, null },
25 | new object[]{ new SortedDictionary() { { 1, 100 } }, null },
26 | };
27 |
28 | [Theory]
29 | [MemberData(nameof(dictionaryTestData))]
30 | public void DictionaryTestAll(T x, T y)
31 | {
32 | Convert(x).IsStructuralEqual(x);
33 | Convert(y).IsStructuralEqual(y);
34 | }
35 |
36 | [Fact]
37 | public void InterfaceDictionaryTest()
38 | {
39 | var a = (IDictionary)new Dictionary() { { 1, 100 } };
40 | var b = (IReadOnlyDictionary)new Dictionary() { { 1, 100 } };
41 | var c = (IDictionary)null;
42 | var d = (IReadOnlyDictionary)null;
43 |
44 | var huga = JsonSerializer.ToJsonString(a);
45 |
46 | Convert(a).IsStructuralEqual(a);
47 | Convert(b).IsStructuralEqual(b);
48 | Convert(c).IsStructuralEqual(c);
49 | Convert(d).IsStructuralEqual(d);
50 | }
51 |
52 | [Fact]
53 | public void ConcurrentDictionaryTest()
54 | {
55 | var cd = new ConcurrentDictionary();
56 |
57 | cd.TryAdd(1, 100);
58 | cd.TryAdd(2, 200);
59 | cd.TryAdd(3, 300);
60 |
61 | var conv = Convert(cd);
62 | conv[1].Is(100);
63 | conv[2].Is(200);
64 | conv[3].Is(300);
65 |
66 | cd = null;
67 | Convert(cd).IsNull();
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/Utf8Json.CodeGenerator/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/src/Utf8Json.UnityClient/ProjectSettings/GraphicsSettings.asset:
--------------------------------------------------------------------------------
1 | %YAML 1.1
2 | %TAG !u! tag:unity3d.com,2011:
3 | --- !u!30 &1
4 | GraphicsSettings:
5 | m_ObjectHideFlags: 0
6 | serializedVersion: 12
7 | m_Deferred:
8 | m_Mode: 1
9 | m_Shader: {fileID: 69, guid: 0000000000000000f000000000000000, type: 0}
10 | m_DeferredReflections:
11 | m_Mode: 1
12 | m_Shader: {fileID: 74, guid: 0000000000000000f000000000000000, type: 0}
13 | m_ScreenSpaceShadows:
14 | m_Mode: 1
15 | m_Shader: {fileID: 64, guid: 0000000000000000f000000000000000, type: 0}
16 | m_LegacyDeferred:
17 | m_Mode: 1
18 | m_Shader: {fileID: 63, guid: 0000000000000000f000000000000000, type: 0}
19 | m_DepthNormals:
20 | m_Mode: 1
21 | m_Shader: {fileID: 62, guid: 0000000000000000f000000000000000, type: 0}
22 | m_MotionVectors:
23 | m_Mode: 1
24 | m_Shader: {fileID: 75, guid: 0000000000000000f000000000000000, type: 0}
25 | m_LightHalo:
26 | m_Mode: 1
27 | m_Shader: {fileID: 105, guid: 0000000000000000f000000000000000, type: 0}
28 | m_LensFlare:
29 | m_Mode: 1
30 | m_Shader: {fileID: 102, guid: 0000000000000000f000000000000000, type: 0}
31 | m_AlwaysIncludedShaders:
32 | - {fileID: 7, guid: 0000000000000000f000000000000000, type: 0}
33 | - {fileID: 15104, guid: 0000000000000000f000000000000000, type: 0}
34 | - {fileID: 15105, guid: 0000000000000000f000000000000000, type: 0}
35 | - {fileID: 15106, guid: 0000000000000000f000000000000000, type: 0}
36 | - {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
37 | - {fileID: 10770, guid: 0000000000000000f000000000000000, type: 0}
38 | m_PreloadedShaders: []
39 | m_SpritesDefaultMaterial: {fileID: 10754, guid: 0000000000000000f000000000000000,
40 | type: 0}
41 | m_CustomRenderPipeline: {fileID: 0}
42 | m_TransparencySortMode: 0
43 | m_TransparencySortAxis: {x: 0, y: 0, z: 1}
44 | m_DefaultRenderingPath: 1
45 | m_DefaultMobileRenderingPath: 1
46 | m_TierSettings: []
47 | m_LightmapStripping: 0
48 | m_FogStripping: 0
49 | m_InstancingStripping: 0
50 | m_LightmapKeepPlain: 1
51 | m_LightmapKeepDirCombined: 1
52 | m_LightmapKeepDynamicPlain: 1
53 | m_LightmapKeepDynamicDirCombined: 1
54 | m_LightmapKeepShadowMask: 1
55 | m_LightmapKeepSubtractive: 1
56 | m_FogKeepLinear: 1
57 | m_FogKeepExp: 1
58 | m_FogKeepExp2: 1
59 | m_AlbedoSwatchInfos: []
60 | m_LightsUseLinearIntensity: 0
61 | m_LightsUseColorTemperature: 0
62 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/Utils/ChainingAssertion.Ext.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using Xunit;
6 |
7 | namespace Xunit
8 | {
9 | public static partial class AssertEx
10 | {
11 | public static void IsZero(this IEnumerable source)
12 | {
13 | source.Any().IsFalse();
14 | }
15 |
16 | /// Alternative of ExpectedExceptionAttribute(allow derived type)
17 | public static T Catch(Action testCode, string message = "") where T : Exception
18 | {
19 | var exception = ExecuteCode(testCode);
20 | var headerMsg = "Failed Throws<" + typeof(T).Name + ">.";
21 | var additionalMsg = string.IsNullOrEmpty(message) ? "" : ", " + message;
22 |
23 | if (exception == null)
24 | {
25 | var formatted = headerMsg + " No exception was thrown" + additionalMsg;
26 | throw new AssertFailedException(formatted);
27 | }
28 | else if (!typeof(T).GetTypeInfo().IsInstanceOfType(exception))
29 | {
30 | var formatted = string.Format("{0} Catched:{1}{2}", headerMsg, exception.GetType().Name, additionalMsg);
31 | throw new AssertFailedException(formatted);
32 | }
33 |
34 | return (T)exception;
35 | }
36 |
37 | public static T Throws(Action testCode, string message = "") where T : Exception
38 | {
39 | var exception = Catch(testCode, message);
40 |
41 | if (!typeof(T).Equals(exception.GetType()))
42 | {
43 | var headerMsg = "Failed Throws<" + typeof(T).Name + ">.";
44 | var additionalMsg = string.IsNullOrEmpty(message) ? "" : ", " + message;
45 | var formatted = string.Format("{0} Catched:{1}{2}", headerMsg, exception.GetType().Name, additionalMsg);
46 | throw new AssertFailedException(formatted);
47 | }
48 |
49 | return (T)exception;
50 | }
51 |
52 | /// execute action and return exception when catched otherwise return null
53 | private static Exception ExecuteCode(Action testCode)
54 | {
55 | try
56 | {
57 | testCode();
58 | return null;
59 | }
60 | catch (Exception e)
61 | {
62 | return e;
63 | }
64 | }
65 | }
66 | }
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/StreamNonMemoryStraemDeserialization.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.IO;
4 | using System.Text;
5 | using Xunit;
6 |
7 | namespace Utf8Json.Tests
8 | {
9 | public class StreamNonMemoryStraemDeserializationTest
10 | {
11 | [Fact]
12 | public void ConcurrencyCheck()
13 | {
14 | {
15 | var ms = new NonMemoryStream(new MemoryStream(JsonSerializer.Serialize(123)));
16 | var d = JsonSerializer.Deserialize(ms);
17 | d.Is(123);
18 | }
19 | {
20 | var ms = new NonMemoryStream(new MemoryStream(JsonSerializer.Serialize(9)));
21 | var d = JsonSerializer.Deserialize(ms);
22 | d.Is(9);
23 | }
24 | {
25 | var ms = new MemoryStream(JsonSerializer.Serialize(123), 0, 3, true, true);
26 | var d = JsonSerializer.Deserialize(ms);
27 | d.Is(123);
28 | ms.Position = 0;
29 | ms.SetLength(1);
30 | JsonSerializer.Serialize(ms, 1);
31 | var d2 = JsonSerializer.Deserialize(ms);
32 | d2.Is(1);
33 | }
34 | }
35 | }
36 |
37 | class NonMemoryStream : Stream
38 | {
39 | MemoryStream ms;
40 |
41 | public NonMemoryStream(MemoryStream ms)
42 | {
43 | this.ms = ms;
44 | }
45 |
46 | public override bool CanRead => ms.CanRead;
47 |
48 | public override bool CanSeek => ms.CanSeek;
49 |
50 | public override bool CanWrite => ms.CanWrite;
51 |
52 | public override long Length => ms.Length;
53 |
54 | public override long Position { get => ms.Position; set => ms.Position = value; }
55 |
56 | public override void Flush()
57 | {
58 | ms.Flush();
59 | }
60 |
61 | public override int Read(byte[] buffer, int offset, int count)
62 | {
63 | return ms.Read(buffer, offset, count);
64 | }
65 |
66 | public override long Seek(long offset, SeekOrigin origin)
67 | {
68 | return ms.Seek(offset, origin);
69 | }
70 |
71 | public override void SetLength(long value)
72 | {
73 | ms.SetLength(value);
74 | }
75 |
76 | public override void Write(byte[] buffer, int offset, int count)
77 | {
78 | ms.Write(buffer, offset, count);
79 | }
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/Emit/DynamicAssembly.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Reflection;
3 | using System.Reflection.Emit;
4 |
5 | namespace Utf8Json.Internal.Emit
6 | {
7 | internal class DynamicAssembly
8 | {
9 | #if NET45 || NET47
10 | readonly string moduleName;
11 | #endif
12 | readonly AssemblyBuilder assemblyBuilder;
13 | readonly ModuleBuilder moduleBuilder;
14 |
15 | // don't expose ModuleBuilder
16 | // public ModuleBuilder ModuleBuilder { get { return moduleBuilder; } }
17 |
18 | readonly object gate = new object();
19 |
20 | // requires lock on mono environment. see: https://github.com/neuecc/MessagePack-CSharp/issues/161
21 |
22 | public TypeBuilder DefineType(string name, TypeAttributes attr)
23 | {
24 | lock (gate)
25 | {
26 | return moduleBuilder.DefineType(name, attr);
27 | }
28 | }
29 |
30 | public TypeBuilder DefineType(string name, TypeAttributes attr, Type parent)
31 | {
32 | lock (gate)
33 | {
34 | return moduleBuilder.DefineType(name, attr, parent);
35 | }
36 | }
37 |
38 | public TypeBuilder DefineType(string name, TypeAttributes attr, Type parent, Type[] interfaces)
39 | {
40 | lock (gate)
41 | {
42 | return moduleBuilder.DefineType(name, attr, parent, interfaces);
43 | }
44 | }
45 |
46 | public DynamicAssembly(string moduleName)
47 | {
48 | #if NET45 || NET47
49 | this.moduleName = moduleName;
50 | this.assemblyBuilder = System.AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.RunAndSave);
51 | this.moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName, moduleName + ".dll");
52 | #else
53 | #if NETSTANDARD
54 | this.assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.Run);
55 | #else
56 | this.assemblyBuilder = System.AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(moduleName), AssemblyBuilderAccess.Run);
57 | #endif
58 |
59 | this.moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
60 | #endif
61 | }
62 |
63 | #if NET45 || NET47
64 |
65 | public AssemblyBuilder Save()
66 | {
67 | assemblyBuilder.Save(moduleName + ".dll");
68 | return assemblyBuilder;
69 | }
70 |
71 | #endif
72 | }
73 | }
--------------------------------------------------------------------------------
/src/Utf8Json.CodeGenerator/Generator/ResolverTemplate.tt:
--------------------------------------------------------------------------------
1 | <#@ template language="C#" #>
2 | <#@ assembly name="System.Core" #>
3 | <#@ import namespace="System.Linq" #>
4 | <#@ import namespace="System.Text" #>
5 | <#@ import namespace="System.Collections.Generic" #>
6 | #pragma warning disable 618
7 | #pragma warning disable 612
8 | #pragma warning disable 414
9 | #pragma warning disable 168
10 |
11 | namespace <#= Namespace #>
12 | {
13 | using System;
14 | using Utf8Json;
15 |
16 | public class <#= ResolverName #> : global::Utf8Json.IJsonFormatterResolver
17 | {
18 | public static readonly global::Utf8Json.IJsonFormatterResolver Instance = new <#= ResolverName #>();
19 |
20 | <#= ResolverName #>()
21 | {
22 |
23 | }
24 |
25 | public global::Utf8Json.IJsonFormatter GetFormatter()
26 | {
27 | return FormatterCache.formatter;
28 | }
29 |
30 | static class FormatterCache
31 | {
32 | public static readonly global::Utf8Json.IJsonFormatter formatter;
33 |
34 | static FormatterCache()
35 | {
36 | var f = <#= ResolverName #>GetFormatterHelper.GetFormatter(typeof(T));
37 | if (f != null)
38 | {
39 | formatter = (global::Utf8Json.IJsonFormatter)f;
40 | }
41 | }
42 | }
43 | }
44 |
45 | internal static class <#= ResolverName #>GetFormatterHelper
46 | {
47 | static readonly global::System.Collections.Generic.Dictionary lookup;
48 |
49 | static <#= ResolverName #>GetFormatterHelper()
50 | {
51 | lookup = new global::System.Collections.Generic.Dictionary(<#= registerInfos.Length #>)
52 | {
53 | <# for(var i = 0; i < registerInfos.Length; i++) { var x = registerInfos[i]; #>
54 | {typeof(<#= x.FullName #>), <#= i #> },
55 | <# } #>
56 | };
57 | }
58 |
59 | internal static object GetFormatter(Type t)
60 | {
61 | int key;
62 | if (!lookup.TryGetValue(t, out key)) return null;
63 |
64 | switch (key)
65 | {
66 | <# for(var i = 0; i < registerInfos.Length; i++) { var x = registerInfos[i]; #>
67 | case <#= i #>: return new <#= x.FormatterName.StartsWith("global::") ? x.FormatterName: (!string.IsNullOrEmpty(FormatterNamespace) ? FormatterNamespace + "." : FormatterNamespace) + x.FormatterName#>();
68 | <# } #>
69 | default: return null;
70 | }
71 | }
72 | }
73 | }
74 |
75 | #pragma warning disable 168
76 | #pragma warning restore 414
77 | #pragma warning restore 618
78 | #pragma warning restore 612
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/ExceptionTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Xunit;
3 |
4 | namespace Utf8Json.Tests
5 | {
6 | public class FooException : Exception
7 | {
8 | public int Bar { get; set; }
9 |
10 | public FooException() : base("BCD")
11 | {
12 |
13 | }
14 | }
15 |
16 | public class ExceptionTest
17 | {
18 | [Fact]
19 | public void Root1()
20 | {
21 | {
22 | var ex = new Exception("ABC");
23 | var json = JsonSerializer.ToJsonString(ex);
24 | json.Is("{\"ClassName\":\"System.Exception\",\"Message\":\"ABC\",\"StackTrace\":null,\"Source\":null,\"InnerException\":null}");
25 | }
26 | {
27 | var ex = new Exception("ABC");
28 | var json = JsonSerializer.ToJsonString(ex, Utf8Json.Resolvers.StandardResolver.ExcludeNullSnakeCase);
29 | json.Is("{\"class_name\":\"System.Exception\",\"message\":\"ABC\"}");
30 | }
31 | }
32 |
33 | [Fact]
34 | public void Root2()
35 | {
36 | {
37 | FooException ex = new FooException { Bar = 100 };
38 |
39 | var json = JsonSerializer.ToJsonString(ex);
40 | json.Is("{\"ClassName\":\"Utf8Json.Tests.FooException\",\"Bar\":100,\"Message\":\"BCD\",\"StackTrace\":null,\"Source\":null,\"InnerException\":null}");
41 | }
42 | {
43 | Exception ex = new FooException { Bar = 100 };
44 |
45 | var json = JsonSerializer.ToJsonString(ex, Utf8Json.Resolvers.StandardResolver.ExcludeNullSnakeCase);
46 | json.Is("{\"class_name\":\"Utf8Json.Tests.FooException\",\"bar\":100,\"message\":\"BCD\"}");
47 | }
48 | }
49 |
50 | [Fact]
51 | public void Inner()
52 | {
53 | {
54 | var ex = new Exception("ABC", new FooException { Bar = 100 });
55 |
56 | var json = JsonSerializer.ToJsonString(ex);
57 | json.Is("{\"ClassName\":\"System.Exception\",\"Message\":\"ABC\",\"StackTrace\":null,\"Source\":null,\"InnerException\":{\"ClassName\":\"Utf8Json.Tests.FooException\",\"Bar\":100,\"Message\":\"BCD\",\"StackTrace\":null,\"Source\":null,\"InnerException\":null}}");
58 | }
59 | {
60 | var ex = new Exception("ABC", new FooException { Bar = 100 });
61 |
62 | var json = JsonSerializer.ToJsonString(ex, Utf8Json.Resolvers.StandardResolver.ExcludeNullSnakeCase);
63 | json.Is("{\"class_name\":\"System.Exception\",\"message\":\"ABC\",\"inner_exception\":{\"class_name\":\"Utf8Json.Tests.FooException\",\"bar\":100,\"message\":\"BCD\"}}");
64 | }
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/Utf8Json/Formatters/NullableFormatter.cs:
--------------------------------------------------------------------------------
1 |
2 | using System;
3 |
4 | namespace Utf8Json.Formatters
5 | {
6 | public sealed class NullableFormatter : IJsonFormatter
7 | where T : struct
8 | {
9 | public void Serialize(ref JsonWriter writer, T? value, IJsonFormatterResolver formatterResolver)
10 | {
11 | if (value == null)
12 | {
13 | writer.WriteNull();
14 | }
15 | else
16 | {
17 | formatterResolver.GetFormatterWithVerify().Serialize(ref writer, value.Value, formatterResolver);
18 | }
19 | }
20 |
21 | public T? Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
22 | {
23 | if (reader.ReadIsNull())
24 | {
25 | return null;
26 | }
27 | else
28 | {
29 | return formatterResolver.GetFormatterWithVerify().Deserialize(ref reader, formatterResolver);
30 | }
31 | }
32 | }
33 |
34 | public sealed class StaticNullableFormatter : IJsonFormatter
35 | where T : struct
36 | {
37 | readonly IJsonFormatter underlyingFormatter;
38 |
39 | public StaticNullableFormatter(IJsonFormatter underlyingFormatter)
40 | {
41 | this.underlyingFormatter = underlyingFormatter;
42 | }
43 |
44 | public StaticNullableFormatter(Type formatterType, object[] formatterArguments)
45 | {
46 | try
47 | {
48 | underlyingFormatter = (IJsonFormatter)Activator.CreateInstance(formatterType, formatterArguments);
49 | }
50 | catch (Exception ex)
51 | {
52 | throw new InvalidOperationException("Can not create formatter from JsonFormatterAttribute, check the target formatter is public and has constructor with right argument. FormatterType:" + formatterType.Name, ex);
53 | }
54 | }
55 |
56 | public void Serialize(ref JsonWriter writer, T? value, IJsonFormatterResolver formatterResolver)
57 | {
58 | if (value == null)
59 | {
60 | writer.WriteNull();
61 | }
62 | else
63 | {
64 | underlyingFormatter.Serialize(ref writer, value.Value, formatterResolver);
65 | }
66 | }
67 |
68 | public T? Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
69 | {
70 | if (reader.ReadIsNull())
71 | {
72 | return null;
73 | }
74 | else
75 | {
76 | return underlyingFormatter.Deserialize(ref reader, formatterResolver);
77 | }
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/docs/Utf8Json.nuspec:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Utf8Json
5 | 1.3.7
6 | Utf8Json
7 | neuecc
8 | neuecc
9 | https://github.com/neuecc/Utf8Json/
10 | false
11 | Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
12 | Json, Serialization, Formatter, Serializer, Unity, Xamarin
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/sandbox/PerfBenchmark/Benchmark2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Utf8Json;
5 | using Jil;
6 | using BenchmarkDotNet.Attributes;
7 | using BenchmarkDotNet.Running;
8 | using BenchmarkDotNet.Attributes.Jobs;
9 |
10 | namespace PerfBenchmark
11 | {
12 | public struct MyModel
13 | {
14 | public string X { get; set; }
15 | public string Y { get; set; }
16 | public int Z { get; set; }
17 | }
18 | public class JsonSerializeBench
19 | {
20 | [Params(1, 10000)]
21 | public int LoopNum { get; set; }
22 |
23 | static string json;
24 | static byte[] jsonBytes;
25 |
26 | public JsonSerializeBench()
27 | {
28 | var m = new MyModel();
29 | m.X = $"abcdefg";
30 | m.Y = $"@aiweraw";
31 | m.Z = int.MaxValue;
32 | json = Jil.JSON.Serialize(m);
33 | jsonBytes = Encoding.UTF8.GetBytes(json);
34 | }
35 |
36 | [Benchmark]
37 | public void JilSerialize_String()
38 | {
39 | var m = new MyModel();
40 | m.X = $"abcdefg";
41 | m.Y = $"@aiweraw";
42 | m.Z = int.MaxValue;
43 | for (int i = 0; i < LoopNum; i++)
44 | {
45 | Jil.JSON.Serialize(m);
46 | }
47 | }
48 | [Benchmark]
49 | public void JilSerialize_String_UTf8()
50 | {
51 | var m = new MyModel();
52 | m.X = $"abcdefg";
53 | m.Y = $"@aiweraw";
54 | m.Z = int.MaxValue;
55 | for (int i = 0; i < LoopNum; i++)
56 | {
57 | Encoding.UTF8.GetBytes(Jil.JSON.Serialize(m));
58 | }
59 | }
60 | [Benchmark]
61 | public void Utf8JsonSerialize()
62 | {
63 | var m = new MyModel();
64 | m.X = $"abcdefg";
65 | m.Y = $"@aiweraw";
66 | m.Z = int.MaxValue;
67 | for (int i = 0; i < LoopNum; i++)
68 | {
69 | Utf8Json.JsonSerializer.Serialize(m);
70 | }
71 | }
72 | [Benchmark]
73 | public void JilJsonDeserialize_String()
74 | {
75 | for (int i = 0; i < LoopNum; i++)
76 | {
77 | Jil.JSON.Deserialize(json);
78 | }
79 | }
80 | [Benchmark]
81 | public void JilJsonDeserialize_Utf8()
82 | {
83 | for (int i = 0; i < LoopNum; i++)
84 | {
85 | Jil.JSON.Deserialize(Encoding.UTF8.GetString(jsonBytes));
86 | }
87 | }
88 | [Benchmark]
89 | public void Utf8JsonDeserialize()
90 | {
91 | for (int i = 0; i < LoopNum; i++)
92 | {
93 | Utf8Json.JsonSerializer.Deserialize(jsonBytes);
94 | }
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/src/Utf8Json.AspNetCoreMvcFormatter/Formatter.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc.Formatters;
2 | using System.Threading.Tasks;
3 |
4 | namespace Utf8Json.AspNetCoreMvcFormatter
5 | {
6 | public class JsonOutputFormatter : IOutputFormatter //, IApiResponseTypeMetadataProvider
7 | {
8 | const string ContentType = "application/json";
9 | static readonly string[] SupportedContentTypes = new[] { ContentType };
10 |
11 | readonly IJsonFormatterResolver resolver;
12 |
13 | public JsonOutputFormatter()
14 | : this(null)
15 | {
16 |
17 | }
18 | public JsonOutputFormatter(IJsonFormatterResolver resolver)
19 | {
20 | this.resolver = resolver ?? JsonSerializer.DefaultResolver;
21 | }
22 |
23 | //public IReadOnlyList GetSupportedContentTypes(string contentType, Type objectType)
24 | //{
25 | // return SupportedContentTypes;
26 | //}
27 |
28 | public bool CanWriteResult(OutputFormatterCanWriteContext context)
29 | {
30 | return true;
31 | }
32 |
33 | public Task WriteAsync(OutputFormatterWriteContext context)
34 | {
35 | context.HttpContext.Response.ContentType = ContentType;
36 |
37 | // when 'object' use the concrete type(object.GetType())
38 | if (context.ObjectType == typeof(object))
39 | {
40 | return JsonSerializer.NonGeneric.SerializeAsync(context.HttpContext.Response.Body, context.Object, resolver);
41 | }
42 | else
43 | {
44 | return JsonSerializer.NonGeneric.SerializeAsync(context.ObjectType, context.HttpContext.Response.Body, context.Object, resolver);
45 | }
46 | }
47 | }
48 |
49 | public class JsonInputFormatter : IInputFormatter // , IApiRequestFormatMetadataProvider
50 | {
51 | const string ContentType = "application/json";
52 | static readonly string[] SupportedContentTypes = new[] { ContentType };
53 |
54 | readonly IJsonFormatterResolver resolver;
55 |
56 | public JsonInputFormatter()
57 | : this(null)
58 | {
59 |
60 | }
61 |
62 | public JsonInputFormatter(IJsonFormatterResolver resolver)
63 | {
64 | this.resolver = resolver ?? JsonSerializer.DefaultResolver;
65 | }
66 |
67 | //public IReadOnlyList GetSupportedContentTypes(string contentType, Type objectType)
68 | //{
69 | // return SupportedContentTypes;
70 | //}
71 |
72 | public bool CanRead(InputFormatterContext context)
73 | {
74 | return true;
75 | }
76 |
77 | public Task ReadAsync(InputFormatterContext context)
78 | {
79 | var request = context.HttpContext.Request;
80 | var result = JsonSerializer.NonGeneric.Deserialize(context.ModelType, request.Body, resolver);
81 | return InputFormatterResult.SuccessAsync(result);
82 | }
83 | }
84 | }
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/ReflectionExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Reflection;
4 | using System.Runtime.CompilerServices;
5 | using System.Text;
6 |
7 | namespace Utf8Json.Internal
8 | {
9 | internal static class ReflectionExtensions
10 | {
11 | public static bool IsNullable(this System.Reflection.TypeInfo type)
12 | {
13 | return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Nullable<>);
14 | }
15 |
16 | public static bool IsPublic(this System.Reflection.TypeInfo type)
17 | {
18 | return type.IsPublic;
19 | }
20 |
21 | public static bool IsAnonymous(this System.Reflection.TypeInfo type)
22 | {
23 | return type.GetCustomAttribute() != null
24 | && type.Name.Contains("AnonymousType")
25 | && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$"))
26 | && (type.Attributes & TypeAttributes.NotPublic) == TypeAttributes.NotPublic;
27 | }
28 |
29 | public static IEnumerable GetAllProperties(this Type type)
30 | {
31 | return GetAllPropertiesCore(type, new HashSet());
32 | }
33 |
34 | static IEnumerable GetAllPropertiesCore(Type type, HashSet nameCheck)
35 | {
36 | foreach (var item in type.GetRuntimeProperties())
37 | {
38 | if (nameCheck.Add(item.Name))
39 | {
40 | yield return item;
41 | }
42 | }
43 | if (type.BaseType != null)
44 | {
45 | foreach (var item in GetAllPropertiesCore(type.BaseType, nameCheck))
46 | {
47 | yield return item;
48 | }
49 | }
50 | }
51 |
52 | public static IEnumerable GetAllFields(this Type type)
53 | {
54 | return GetAllFieldsCore(type, new HashSet());
55 | }
56 |
57 | static IEnumerable GetAllFieldsCore(Type type, HashSet nameCheck)
58 | {
59 | foreach (var item in type.GetRuntimeFields())
60 | {
61 | if (nameCheck.Add(item.Name))
62 | {
63 | yield return item;
64 | }
65 | }
66 | if (type.BaseType != null)
67 | {
68 | foreach (var item in GetAllFieldsCore(type.BaseType, nameCheck))
69 | {
70 | yield return item;
71 | }
72 | }
73 | }
74 |
75 | #if NETSTANDARD
76 |
77 | public static bool IsConstructedGenericType(this System.Reflection.TypeInfo type)
78 | {
79 | return type.AsType().IsConstructedGenericType;
80 | }
81 |
82 | public static MethodInfo GetGetMethod(this PropertyInfo propInfo)
83 | {
84 | return propInfo.GetMethod;
85 | }
86 |
87 | public static MethodInfo GetSetMethod(this PropertyInfo propInfo)
88 | {
89 | return propInfo.SetMethod;
90 | }
91 |
92 | #endif
93 | }
94 | }
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/Emit/ExpressionUtility.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq.Expressions;
3 | using System.Reflection;
4 |
5 | namespace Utf8Json.Internal.Emit
6 | {
7 | internal static class ExpressionUtility
8 | {
9 | // Method
10 |
11 | static MethodInfo GetMethodInfoCore(LambdaExpression expression)
12 | {
13 | if (expression == null)
14 | {
15 | throw new ArgumentNullException("expression");
16 | }
17 |
18 | return (expression.Body as MethodCallExpression).Method;
19 | }
20 |
21 | ///
22 | /// Get MethodInfo from Expression for Static(with result) method.
23 | ///
24 | public static MethodInfo GetMethodInfo(Expression> expression)
25 | {
26 | return GetMethodInfoCore(expression);
27 | }
28 |
29 | ///
30 | /// Get MethodInfo from Expression for Static(void) method.
31 | ///
32 | public static MethodInfo GetMethodInfo(Expression expression)
33 | {
34 | return GetMethodInfoCore(expression);
35 | }
36 |
37 | ///
38 | /// Get MethodInfo from Expression for Instance(with result) method.
39 | ///
40 | public static MethodInfo GetMethodInfo(Expression> expression)
41 | {
42 | return GetMethodInfoCore(expression);
43 | }
44 |
45 | ///
46 | /// Get MethodInfo from Expression for Instance(void) method.
47 | ///
48 | public static MethodInfo GetMethodInfo(Expression> expression)
49 | {
50 | return GetMethodInfoCore(expression);
51 | }
52 |
53 | // WithArgument(for ref, out) helper
54 |
55 | ///
56 | /// Get MethodInfo from Expression for Instance(void) method.
57 | ///
58 | public static MethodInfo GetMethodInfo(Expression> expression)
59 | {
60 | return GetMethodInfoCore(expression);
61 | }
62 |
63 | ///
64 | /// Get MethodInfo from Expression for Instance(with result) method.
65 | ///
66 | public static MethodInfo GetMethodInfo(Expression> expression)
67 | {
68 | return GetMethodInfoCore(expression);
69 | }
70 |
71 | // Property
72 |
73 | static MemberInfo GetMemberInfoCore(Expression source)
74 | {
75 | if (source == null)
76 | {
77 | throw new ArgumentNullException("source");
78 | }
79 |
80 | var memberExpression = source.Body as MemberExpression;
81 | return memberExpression.Member;
82 | }
83 |
84 | public static PropertyInfo GetPropertyInfo(Expression> expression)
85 | {
86 | return GetMemberInfoCore(expression) as PropertyInfo;
87 | }
88 |
89 | // Field
90 |
91 | public static FieldInfo GetFieldInfo(Expression> expression)
92 | {
93 | return GetMemberInfoCore(expression) as FieldInfo;
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/NonGenericTest.cs:
--------------------------------------------------------------------------------
1 | using SharedData;
2 | using System.Linq;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.IO;
6 | using System.Text;
7 | using Xunit;
8 | using Utf8Json.Resolvers;
9 |
10 | namespace Utf8Json.Tests
11 | {
12 | public class NonGenericTest
13 | {
14 | T ConvertNonGeneric(T obj)
15 | {
16 | var t = obj.GetType();
17 | return (T)JsonSerializer.NonGeneric.Deserialize(t, JsonSerializer.NonGeneric.Serialize(t, obj));
18 | }
19 |
20 | [Fact]
21 | public void NonGeneric()
22 | {
23 | var data = new FirstSimpleData { Prop1 = 9, Prop2 = "hoge", Prop3 = 999 };
24 | var t = typeof(FirstSimpleData);
25 | var ms = new MemoryStream();
26 |
27 | var data1 = JsonSerializer.NonGeneric.Deserialize(t, JsonSerializer.NonGeneric.Serialize(t, data)) as FirstSimpleData;
28 | var data2 = JsonSerializer.NonGeneric.Deserialize(t, JsonSerializer.NonGeneric.Serialize(t, data , StandardResolver.Default)) as FirstSimpleData;
29 |
30 | JsonSerializer.NonGeneric.Serialize(t, ms, data);
31 | ms.Position = 0;
32 | var data3 = JsonSerializer.NonGeneric.Deserialize(t, ms) as FirstSimpleData;
33 |
34 | ms = new MemoryStream();
35 | JsonSerializer.NonGeneric.Serialize(t, ms, data);
36 | ms.Position = 0;
37 | var data4 = JsonSerializer.NonGeneric.Deserialize(t, ms, StandardResolver.Default) as FirstSimpleData;
38 |
39 | new[] { data1.Prop1, data2.Prop1, data3.Prop1, data4.Prop1 }.Distinct().Is(data.Prop1);
40 | new[] { data1.Prop2, data2.Prop2, data3.Prop2, data4.Prop2 }.Distinct().Is(data.Prop2);
41 | new[] { data1.Prop3, data2.Prop3, data3.Prop3, data4.Prop3 }.Distinct().Is(data.Prop3);
42 | }
43 |
44 | [Fact]
45 | public void WriterReader()
46 | {
47 | var data = new FirstSimpleData { Prop1 = 9, Prop2 = "hoge", Prop3 = 999 };
48 | var t = typeof(FirstSimpleData);
49 |
50 | var data1 = JsonSerializer.NonGeneric.Deserialize(t, JsonSerializer.NonGeneric.Serialize(t, data)) as FirstSimpleData;
51 | var data2 = JsonSerializer.NonGeneric.Deserialize(t, JsonSerializer.NonGeneric.Serialize(t, data, StandardResolver.Default)) as FirstSimpleData;
52 |
53 |
54 | var ms = new JsonWriter();
55 | JsonSerializer.NonGeneric.Serialize(t, ref ms, data);
56 | var ms2 = new JsonReader(ms.ToUtf8ByteArray());
57 | var data3 = JsonSerializer.NonGeneric.Deserialize(t, ref ms2) as FirstSimpleData;
58 |
59 | ms = new JsonWriter();
60 | JsonSerializer.NonGeneric.Serialize(t, ref ms, data, StandardResolver.Default);
61 | ms2 = new JsonReader(ms.ToUtf8ByteArray());
62 | var data4 = JsonSerializer.NonGeneric.Deserialize(t, ref ms2, StandardResolver.Default) as FirstSimpleData;
63 |
64 | new[] { data1.Prop1, data2.Prop1, data3.Prop1, data4.Prop1 }.Distinct().Is(data.Prop1);
65 | new[] { data1.Prop2, data2.Prop2, data3.Prop2, data4.Prop2 }.Distinct().Is(data.Prop2);
66 | new[] { data1.Prop3, data2.Prop3, data3.Prop3, data4.Prop3 }.Distinct().Is(data.Prop3);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/sandbox/DynamicCodeDumper/DynamicCodeDumper.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {7311C780-E172-45CE-BF8C-81C9DFFEB5F5}
8 | Exe
9 | DynamicCodeDumper
10 | DynamicCodeDumper
11 | v4.7
12 | 512
13 | true
14 |
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | TRACE;DEBUG;NET45
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 | true
37 | bin\DumpAsm\
38 | TRACE;DEBUG;NET_35;NET_STANDARD;DUMP_ASM
39 | full
40 | AnyCPU
41 | prompt
42 | MinimumRecommendedRules.ruleset
43 | false
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | {ae4b8434-240d-4aae-a35d-7bb664271ebc}
66 | Utf8Json
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/GenericFormatter.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using Xunit;
5 |
6 | namespace Utf8Json.Tests
7 | {
8 | public class GenericFormatters
9 | {
10 | T Convert(T value)
11 | {
12 | return JsonSerializer.Deserialize(JsonSerializer.Serialize(value));
13 | }
14 |
15 | public static object[] tupleTestData = new object[]
16 | {
17 | new object[] { Tuple.Create(1) },
18 | new object[] { Tuple.Create(1,2) },
19 | new object[] { Tuple.Create(1,2,3) },
20 | new object[] { Tuple.Create(1,2,3,4) },
21 | new object[] { Tuple.Create(1,2,3,4,5) },
22 | new object[] { Tuple.Create(1,2,3,4,5,6) },
23 | new object[] { Tuple.Create(1,2,3,4,5,6,7) },
24 | new object[] { Tuple.Create(1,2,3,4,5,6,7,8) },
25 | };
26 |
27 | [Theory]
28 | [MemberData(nameof(tupleTestData))]
29 | public void TupleTest(T data)
30 | {
31 | Convert(data).IsStructuralEqual(data);
32 | }
33 |
34 | public static object[] valueTupleTestData = new object[]
35 | {
36 | new object[] { ValueTuple.Create(1),null },
37 | new object[] { ValueTuple.Create(1,2),null },
38 | new object[] { ValueTuple.Create(1,2,3),null },
39 | new object[] { ValueTuple.Create(1,2,3,4),null },
40 | new object[] { ValueTuple.Create(1,2,3,4,5) ,null},
41 | new object[] { ValueTuple.Create(1,2,3,4,5,6) ,null},
42 | new object[] { ValueTuple.Create(1,2,3,4,5,6,7) ,null},
43 | new object[] { ValueTuple.Create(1,2,3,4,5,6,7,8) ,null},
44 | };
45 |
46 | [Theory]
47 | [MemberData(nameof(valueTupleTestData))]
48 | public void TupleTest(T data, T? @nullData)
49 | where T : struct
50 | {
51 | Convert(data).IsStructuralEqual(data);
52 | Convert(@nullData).IsNull();
53 | }
54 |
55 | public static object[] keyValuePairData = new object[]
56 | {
57 | new object[] { new KeyValuePair(1,2), null },
58 | new object[] { new KeyValuePair(3,4), new KeyValuePair(5,6) },
59 | };
60 |
61 | [Theory]
62 | [MemberData(nameof(keyValuePairData))]
63 | public void KeyValuePairTest(T t, T? t2)
64 | where T : struct
65 | {
66 | Convert(t).IsStructuralEqual(t);
67 | Convert(t2).IsStructuralEqual(t2);
68 | }
69 |
70 | public static object[] byteArraySegementData = new object[]
71 | {
72 | new object[] { new ArraySegment(new byte[] { 0, 0, 1, 2, 3 }, 2, 3), null, new byte[] { 1, 2, 3 } },
73 | new object[] { new ArraySegment(new byte[0], 0, 0), null, new byte[0] },
74 | };
75 |
76 | [Theory]
77 | [MemberData(nameof(byteArraySegementData))]
78 | public void ByteArraySegmentTest(ArraySegment t, ArraySegment? t2, byte[] reference)
79 | {
80 | JsonSerializer.Serialize(t).Is(JsonSerializer.Serialize(reference));
81 | Convert(t).Array.Is(reference);
82 | // MessagePackBinary.IsNil(JsonSerializer.Serialize(t2), 0).IsTrue();
83 | }
84 |
85 |
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/sandbox/PerfBenchmark/TargetClass.cs:
--------------------------------------------------------------------------------
1 | using MessagePack;
2 | using Utf8Json.Internal;
3 | using System;
4 | using System.Collections.Generic;
5 | using System.Runtime.InteropServices;
6 | using System.Text;
7 | using Utf8Json;
8 |
9 | namespace PerfBenchmark
10 | {
11 | [StructLayout(LayoutKind.Explicit, Pack = 1)]
12 | public struct LongUnion
13 | {
14 | [FieldOffset(0)]
15 | public int Int1;
16 | [FieldOffset(1)]
17 | public int Int2;
18 |
19 | [FieldOffset(0)]
20 | public float Float;
21 |
22 | [FieldOffset(0)]
23 | public double Double;
24 |
25 | [FieldOffset(0)]
26 | public ulong Long;
27 | }
28 |
29 | [MessagePackObject]
30 | [ProtoBuf.ProtoContract]
31 | public class TargetClass
32 | {
33 | [Key(0)]
34 | [ProtoBuf.ProtoMember(1)]
35 | public sbyte Number1 { get; set; }
36 | [Key(1)]
37 | [ProtoBuf.ProtoMember(2)]
38 | public short Number2 { get; set; }
39 | [Key(2)]
40 | [ProtoBuf.ProtoMember(3)]
41 | public int Number3 { get; set; }
42 | [Key(3)]
43 | [ProtoBuf.ProtoMember(4)]
44 | public long Number4 { get; set; }
45 | [Key(4)]
46 | [ProtoBuf.ProtoMember(5)]
47 | public byte Number5 { get; set; }
48 | [Key(5)]
49 | [ProtoBuf.ProtoMember(6)]
50 | public ushort Number6 { get; set; }
51 | [Key(6)]
52 | [ProtoBuf.ProtoMember(7)]
53 | public uint Number7 { get; set; }
54 | [Key(7)]
55 | [ProtoBuf.ProtoMember(8)]
56 | public ulong Number8 { get; set; }
57 |
58 | public static TargetClass Create(Random random)
59 | {
60 | unchecked
61 | {
62 | return new TargetClass
63 | {
64 | Number1 = (sbyte)random.Next(),
65 | Number2 = (short)random.Next(),
66 | Number3 = (int)random.Next(),
67 | Number4 = (long)new LongUnion { Int1 = random.Next(), Int2 = random.Next() }.Long,
68 | Number5 = (byte)random.Next(),
69 | Number6 = (ushort)random.Next(),
70 | Number7 = (uint)random.Next(),
71 | Number8 = (ulong)new LongUnion { Int1 = random.Next(), Int2 = random.Next() }.Long,
72 | };
73 | }
74 | }
75 | }
76 |
77 | public class TargetClassContractless
78 | {
79 | public sbyte Number1 { get; set; }
80 | public short Number2 { get; set; }
81 | public int Number3 { get; set; }
82 | public long Number4 { get; set; }
83 | public byte Number5 { get; set; }
84 | public ushort Number6 { get; set; }
85 | public uint Number7 { get; set; }
86 | public ulong Number8 { get; set; }
87 |
88 | public TargetClassContractless()
89 | {
90 |
91 | }
92 |
93 | public TargetClassContractless(TargetClass tc)
94 | {
95 | this.Number1 = tc.Number1;
96 | this.Number2 = tc.Number2;
97 | this.Number3 = tc.Number3;
98 | this.Number4 = tc.Number4;
99 | this.Number5 = tc.Number5;
100 | this.Number6 = tc.Number6;
101 | this.Number7 = tc.Number7;
102 | this.Number8 = tc.Number8;
103 | }
104 | }
105 | }
--------------------------------------------------------------------------------
/src/Utf8Json.CodeGenerator/Definitions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | public interface IResolverRegisterInfo
8 | {
9 | string FullName { get; }
10 | string FormatterName { get; }
11 | }
12 |
13 | public class ObjectSerializationInfo : IResolverRegisterInfo
14 | {
15 | public string Name { get; set; }
16 | public string FullName { get; set; }
17 | public string Namespace { get; set; }
18 | public bool IsClass { get; set; }
19 | public bool IsStruct { get { return !IsClass; } }
20 | public MemberSerializationInfo[] ConstructorParameters { get; set; }
21 | public MemberSerializationInfo[] Members { get; set; }
22 | public string FormatterName => (Namespace == null ? Name : Namespace + "." + Name) + "Formatter";
23 | public bool HasConstructor { get; set; }
24 |
25 | public int WriteCount
26 | {
27 | get
28 | {
29 | return Members.Count(x => x.IsReadable);
30 | }
31 | }
32 |
33 | public string GetConstructorString()
34 | {
35 | var args = string.Join(", ", ConstructorParameters.Select(x => "__" + x.Name + "__"));
36 | return $"{FullName}({args})";
37 | }
38 | }
39 |
40 | public class MemberSerializationInfo
41 | {
42 | public bool IsProperty { get; set; }
43 | public bool IsField { get; set; }
44 | public bool IsWritable { get; set; }
45 | public bool IsReadable { get; set; }
46 | public string Type { get; set; }
47 | public string Name { get; set; }
48 | public string MemberName { get; set; }
49 | public string ShortTypeName { get; set; }
50 |
51 | readonly HashSet primitiveTypes = new HashSet(new string[]
52 | {
53 | "short",
54 | "int",
55 | "long",
56 | "ushort",
57 | "uint",
58 | "ulong",
59 | "float",
60 | "double",
61 | "bool",
62 | "byte",
63 | "sbyte",
64 | //"char",
65 | //"global::System.DateTime",
66 | //"byte[]",
67 | "string",
68 | });
69 |
70 | public string GetSerializeMethodString()
71 | {
72 | if (primitiveTypes.Contains(Type))
73 | {
74 | return $"writer.Write{ShortTypeName.Replace("[]", "s")}(value.{Name})";
75 | }
76 | else
77 | {
78 | return $"formatterResolver.GetFormatterWithVerify<{Type}>().Serialize(ref writer, value.{Name}, formatterResolver)";
79 | }
80 | }
81 |
82 | public string GetDeserializeMethodString()
83 | {
84 | if (primitiveTypes.Contains(Type))
85 | {
86 | return $"reader.Read{ShortTypeName.Replace("[]", "s")}()";
87 | }
88 | else
89 | {
90 | return $"formatterResolver.GetFormatterWithVerify<{Type}>().Deserialize(ref reader, formatterResolver)";
91 | }
92 | }
93 | }
94 | public class GenericSerializationInfo : IResolverRegisterInfo, IEquatable
95 | {
96 | public string FullName { get; set; }
97 |
98 | public string FormatterName { get; set; }
99 |
100 | public bool Equals(GenericSerializationInfo other)
101 | {
102 | return FullName.Equals(other.FullName);
103 | }
104 |
105 | public override int GetHashCode()
106 | {
107 | return FullName.GetHashCode();
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/Utf8Json/Internal/BinaryUtil.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | #if NETSTANDARD
6 | using System.Runtime.CompilerServices;
7 | #endif
8 |
9 | namespace Utf8Json.Internal
10 | {
11 | public static class BinaryUtil
12 | {
13 | const int ArrayMaxSize = 0x7FFFFFC7; // https://msdn.microsoft.com/en-us/library/system.array
14 |
15 | #if NETSTANDARD
16 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
17 | #endif
18 | public static void EnsureCapacity(ref byte[] bytes, int offset, int appendLength)
19 | {
20 | var newLength = offset + appendLength;
21 |
22 | // If null(most case fisrt time) fill byte.
23 | if (bytes == null)
24 | {
25 | bytes = new byte[newLength];
26 | return;
27 | }
28 |
29 | // like MemoryStream.EnsureCapacity
30 | var current = bytes.Length;
31 | if (newLength > current)
32 | {
33 | int num = newLength;
34 | if (num < 256)
35 | {
36 | num = 256;
37 | FastResize(ref bytes, num);
38 | return;
39 | }
40 |
41 | if (current == ArrayMaxSize)
42 | {
43 | throw new InvalidOperationException("byte[] size reached maximum size of array(0x7FFFFFC7), can not write to single byte[]. Details: https://msdn.microsoft.com/en-us/library/system.array");
44 | }
45 |
46 | var newSize = unchecked((current * 2));
47 | if (newSize < 0) // overflow
48 | {
49 | num = ArrayMaxSize;
50 | }
51 | else
52 | {
53 | if (num < newSize)
54 | {
55 | num = newSize;
56 | }
57 | }
58 |
59 | FastResize(ref bytes, num);
60 | }
61 | }
62 |
63 | // Buffer.BlockCopy version of Array.Resize
64 | #if NETSTANDARD
65 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
66 | #endif
67 | public static void FastResize(ref byte[] array, int newSize)
68 | {
69 | if (newSize < 0) throw new ArgumentOutOfRangeException("newSize");
70 |
71 | byte[] array2 = array;
72 | if (array2 == null)
73 | {
74 | array = new byte[newSize];
75 | return;
76 | }
77 |
78 | if (array2.Length != newSize)
79 | {
80 | byte[] array3 = new byte[newSize];
81 | Buffer.BlockCopy(array2, 0, array3, 0, (array2.Length > newSize) ? newSize : array2.Length);
82 | array = array3;
83 | }
84 | }
85 |
86 | #if NETSTANDARD
87 | [MethodImpl(MethodImplOptions.AggressiveInlining)]
88 | #endif
89 | public static
90 | #if NETSTANDARD
91 | unsafe
92 | #endif
93 | byte[] FastCloneWithResize(byte[] src, int newSize)
94 | {
95 | if (newSize < 0) throw new ArgumentOutOfRangeException("newSize");
96 | if (src.Length < newSize) throw new ArgumentException("length < newSize");
97 |
98 | if (src == null) return new byte[newSize];
99 |
100 | byte[] dst = new byte[newSize];
101 |
102 | #if NETSTANDARD && !NET45
103 | fixed (byte* pSrc = &src[0])
104 | fixed (byte* pDst = &dst[0])
105 | {
106 | Buffer.MemoryCopy(pSrc, pDst, dst.Length, newSize);
107 | }
108 | #else
109 | Buffer.BlockCopy(src, 0, dst, 0, newSize);
110 | #endif
111 |
112 | return dst;
113 | }
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/src/Utf8Json.UniversalCodeGenerator/CodeAnalysis/Definitions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Utf8Json.UniversalCodeGenerator
8 | {
9 | public interface IResolverRegisterInfo
10 | {
11 | string FullName { get; }
12 | string FormatterName { get; }
13 | }
14 |
15 | public class ObjectSerializationInfo : IResolverRegisterInfo
16 | {
17 | public string Name { get; set; }
18 | public string FullName { get; set; }
19 | public string Namespace { get; set; }
20 | public bool IsClass { get; set; }
21 | public bool IsStruct { get { return !IsClass; } }
22 | public MemberSerializationInfo[] ConstructorParameters { get; set; }
23 | public MemberSerializationInfo[] Members { get; set; }
24 | public string FormatterName => (Namespace == null ? Name : Namespace + "." + Name) + "Formatter";
25 | public bool HasConstructor { get; set; }
26 |
27 | public int WriteCount
28 | {
29 | get
30 | {
31 | return Members.Count(x => x.IsReadable);
32 | }
33 | }
34 |
35 | public string GetConstructorString()
36 | {
37 | var args = string.Join(", ", ConstructorParameters.Select(x => "__" + x.Name + "__"));
38 | return $"{FullName}({args})";
39 | }
40 | }
41 |
42 | public class MemberSerializationInfo
43 | {
44 | public bool IsProperty { get; set; }
45 | public bool IsField { get; set; }
46 | public bool IsWritable { get; set; }
47 | public bool IsReadable { get; set; }
48 | public string Type { get; set; }
49 | public string Name { get; set; }
50 | public string MemberName { get; set; }
51 | public string ShortTypeName { get; set; }
52 |
53 | readonly HashSet primitiveTypes = new HashSet(new string[]
54 | {
55 | "short",
56 | "int",
57 | "long",
58 | "ushort",
59 | "uint",
60 | "ulong",
61 | "float",
62 | "double",
63 | "bool",
64 | "byte",
65 | "sbyte",
66 | //"char",
67 | //"global::System.DateTime",
68 | //"byte[]",
69 | "string",
70 | });
71 |
72 | public string GetSerializeMethodString()
73 | {
74 | if (primitiveTypes.Contains(Type))
75 | {
76 | return $"writer.Write{ShortTypeName.Replace("[]", "s")}(value.{MemberName})";
77 | }
78 | else
79 | {
80 | return $"formatterResolver.GetFormatterWithVerify<{Type}>().Serialize(ref writer, value.{MemberName}, formatterResolver)";
81 | }
82 | }
83 |
84 | public string GetDeserializeMethodString()
85 | {
86 | if (primitiveTypes.Contains(Type))
87 | {
88 | return $"reader.Read{ShortTypeName.Replace("[]", "s")}()";
89 | }
90 | else
91 | {
92 | return $"formatterResolver.GetFormatterWithVerify<{Type}>().Deserialize(ref reader, formatterResolver)";
93 | }
94 | }
95 | }
96 | public class GenericSerializationInfo : IResolverRegisterInfo, IEquatable
97 | {
98 | public string FullName { get; set; }
99 |
100 | public string FormatterName { get; set; }
101 |
102 | public bool Equals(GenericSerializationInfo other)
103 | {
104 | return FullName.Equals(other.FullName);
105 | }
106 |
107 | public override int GetHashCode()
108 | {
109 | return FullName.GetHashCode();
110 | }
111 | }
112 | }
--------------------------------------------------------------------------------
/src/Utf8Json.ImmutableCollection/ImmutableCollectionResolver.cs:
--------------------------------------------------------------------------------
1 | using Utf8Json.Formatters;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Collections.Immutable;
5 | using System.Reflection;
6 |
7 | namespace Utf8Json.ImmutableCollection
8 | {
9 | public class ImmutableCollectionResolver : IJsonFormatterResolver
10 | {
11 | public static IJsonFormatterResolver Instance = new ImmutableCollectionResolver();
12 |
13 | ImmutableCollectionResolver()
14 | {
15 |
16 | }
17 |
18 | public IJsonFormatter GetFormatter()
19 | {
20 | return FormatterCache.formatter;
21 | }
22 |
23 | static class FormatterCache
24 | {
25 | public static readonly IJsonFormatter formatter;
26 |
27 | static FormatterCache()
28 | {
29 | formatter = (IJsonFormatter)ImmutableCollectionGetFormatterHelper.GetFormatter(typeof(T));
30 | }
31 | }
32 | }
33 |
34 | internal static class ImmutableCollectionGetFormatterHelper
35 | {
36 | static readonly Dictionary formatterMap = new Dictionary()
37 | {
38 | {typeof(ImmutableArray<>), typeof(ImmutableArrayFormatter<>)},
39 | {typeof(ImmutableList<>), typeof(ImmutableListFormatter<>)},
40 | {typeof(ImmutableDictionary<,>), typeof(ImmutableDictionaryFormatter<,>)},
41 | {typeof(ImmutableHashSet<>), typeof(ImmutableHashSetFormatter<>)},
42 | {typeof(ImmutableSortedDictionary<,>), typeof(ImmutableSortedDictionaryFormatter<,>)},
43 | {typeof(ImmutableSortedSet<>), typeof(ImmutableSortedSetFormatter<>)},
44 | {typeof(ImmutableQueue<>), typeof(ImmutableQueueFormatter<>)},
45 | {typeof(ImmutableStack<>), typeof(ImmutableStackFormatter<>)},
46 | {typeof(IImmutableList<>), typeof(InterfaceImmutableListFormatter<>)},
47 | {typeof(IImmutableDictionary<,>), typeof(InterfaceImmutableDictionaryFormatter<,>)},
48 | {typeof(IImmutableQueue<>), typeof(InterfaceImmutableQueueFormatter<>)},
49 | {typeof(IImmutableSet<>), typeof(InterfaceImmutableSetFormatter<>)},
50 | {typeof(IImmutableStack<>), typeof(InterfaceImmutableStackFormatter<>)},
51 | };
52 |
53 | internal static object GetFormatter(Type t)
54 | {
55 | var ti = t.GetTypeInfo();
56 |
57 | if (ti.IsGenericType)
58 | {
59 | var genericType = ti.GetGenericTypeDefinition();
60 | var genericTypeInfo = genericType.GetTypeInfo();
61 | var isNullable = genericTypeInfo.IsNullable();
62 | var nullableElementType = isNullable ? ti.GenericTypeArguments[0] : null;
63 |
64 | Type formatterType;
65 | if (formatterMap.TryGetValue(genericType, out formatterType))
66 | {
67 | return CreateInstance(formatterType, ti.GenericTypeArguments);
68 | }
69 | else if (isNullable && nullableElementType.IsConstructedGenericType && nullableElementType.GetGenericTypeDefinition() == typeof(ImmutableArray<>))
70 | {
71 | return CreateInstance(typeof(NullableFormatter<>), new[] { nullableElementType });
72 | }
73 | }
74 |
75 | return null;
76 | }
77 |
78 | static object CreateInstance(Type genericType, Type[] genericTypeArguments, params object[] arguments)
79 | {
80 | return Activator.CreateInstance(genericType.MakeGenericType(genericTypeArguments), arguments);
81 | }
82 | }
83 |
84 | internal static class ReflectionExtensions
85 | {
86 | public static bool IsNullable(this System.Reflection.TypeInfo type)
87 | {
88 | return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(System.Nullable<>);
89 | }
90 | }
91 | }
--------------------------------------------------------------------------------
/tests/Utf8Json.Tests/FormatterAttributeTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using Utf8Json.Resolvers;
7 | using Xunit;
8 |
9 | namespace Utf8Json.Tests
10 | {
11 | public class SpecifiedFormatterResolverTest
12 | {
13 | [JsonFormatter(typeof(NoObjectFormatter))]
14 | class CustomClassObject
15 | {
16 | int X;
17 |
18 | public CustomClassObject(int x)
19 | {
20 | this.X = x;
21 | }
22 |
23 | public int GetX()
24 | {
25 | return X;
26 | }
27 |
28 | class NoObjectFormatter : IJsonFormatter
29 | {
30 | public CustomClassObject Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
31 | {
32 | var r = reader.ReadInt32();
33 | return new CustomClassObject(r);
34 | }
35 |
36 | public void Serialize(ref JsonWriter writer, CustomClassObject value, IJsonFormatterResolver formatterResolver)
37 | {
38 | writer.WriteInt32(value.X);
39 | }
40 | }
41 | }
42 |
43 | [JsonFormatter(typeof(CustomStructObjectFormatter))]
44 | struct CustomStructObject
45 | {
46 | int X;
47 |
48 | public CustomStructObject(int x)
49 | {
50 | this.X = x;
51 | }
52 |
53 | public int GetX()
54 | {
55 | return X;
56 | }
57 |
58 | class CustomStructObjectFormatter : IJsonFormatter
59 | {
60 | public CustomStructObject Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
61 | {
62 | var r = reader.ReadInt32();
63 | return new CustomStructObject(r);
64 | }
65 |
66 | public void Serialize(ref JsonWriter writer, CustomStructObject value, IJsonFormatterResolver formatterResolver)
67 | {
68 | writer.WriteInt32(value.X);
69 | }
70 | }
71 | }
72 |
73 |
74 | [JsonFormatter(typeof(CustomInterfaceObjectFormatter))]
75 | interface ICustomInterfaceObject
76 | {
77 | int A { get; }
78 | }
79 |
80 | class CustomInterfaceObjectFormatter : IJsonFormatter
81 | {
82 | public ICustomInterfaceObject Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
83 | {
84 | var r = reader.ReadInt32();
85 | return new InheritDefault(r);
86 | }
87 |
88 | public void Serialize(ref JsonWriter writer, ICustomInterfaceObject value, IJsonFormatterResolver formatterResolver)
89 | {
90 | writer.WriteInt32( value.A);
91 | }
92 | }
93 |
94 | class InheritDefault : ICustomInterfaceObject
95 | {
96 | public int A { get; }
97 |
98 | public InheritDefault(int a)
99 | {
100 | this.A = a;
101 | }
102 | }
103 |
104 | class HogeMoge : ICustomInterfaceObject
105 | {
106 | public int A { get; set; }
107 | }
108 |
109 |
110 | T Convert(T value)
111 | {
112 | return JsonSerializer.Deserialize(JsonSerializer.Serialize(value, StandardResolver.Default), StandardResolver.Default);
113 | }
114 |
115 | [Fact]
116 | public void CustomFormatters()
117 | {
118 | Convert(new CustomClassObject(999)).GetX().Is(999);
119 | Convert(new CustomStructObject(1234)).GetX().Is(1234);
120 | Convert(new HogeMoge { A = 999 }).A.Is(999);
121 | }
122 |
123 | }
124 |
125 | }
126 |
--------------------------------------------------------------------------------
/src/Utf8Json/Resolvers/EnumResolver.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Utf8Json.Internal.Emit;
3 | using Utf8Json.Internal;
4 | using System.Reflection;
5 | using System.Collections.Generic;
6 | using System.Text;
7 | using Utf8Json.Formatters;
8 | using Utf8Json.Resolvers.Internal;
9 |
10 | namespace Utf8Json.Resolvers
11 | {
12 | public static class EnumResolver
13 | {
14 | /// Serialize as Name.
15 | public static readonly IJsonFormatterResolver Default = EnumDefaultResolver.Instance;
16 | /// Serialize as Value.
17 | public static readonly IJsonFormatterResolver UnderlyingValue = EnumUnderlyingValueResolver.Instance;
18 | }
19 | }
20 |
21 | namespace Utf8Json.Resolvers.Internal
22 | {
23 | internal sealed class EnumDefaultResolver : IJsonFormatterResolver
24 | {
25 | public static readonly IJsonFormatterResolver Instance = new EnumDefaultResolver();
26 |
27 | EnumDefaultResolver()
28 | {
29 | }
30 |
31 | public IJsonFormatter GetFormatter()
32 | {
33 | return FormatterCache.formatter;
34 | }
35 |
36 | static class FormatterCache
37 | {
38 | public static readonly IJsonFormatter formatter;
39 |
40 | static FormatterCache()
41 | {
42 | var ti = typeof(T).GetTypeInfo();
43 |
44 | if (ti.IsNullable())
45 | {
46 | // build underlying type and use wrapped formatter.
47 | ti = ti.GenericTypeArguments[0].GetTypeInfo();
48 | if (!ti.IsEnum)
49 | {
50 | return;
51 | }
52 |
53 | var innerFormatter = Instance.GetFormatterDynamic(ti.AsType());
54 | if (innerFormatter == null)
55 | {
56 | return;
57 | }
58 | formatter = (IJsonFormatter)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter });
59 | return;
60 | }
61 | else if (typeof(T).IsEnum)
62 | {
63 | formatter = (IJsonFormatter)(object)new EnumFormatter(true);
64 | }
65 | }
66 | }
67 | }
68 |
69 | internal sealed class EnumUnderlyingValueResolver : IJsonFormatterResolver
70 | {
71 | public static readonly IJsonFormatterResolver Instance = new EnumUnderlyingValueResolver();
72 |
73 | EnumUnderlyingValueResolver()
74 | {
75 | }
76 |
77 | public IJsonFormatter GetFormatter()
78 | {
79 | return FormatterCache.formatter;
80 | }
81 |
82 | static class FormatterCache
83 | {
84 | public static readonly IJsonFormatter formatter;
85 |
86 | static FormatterCache()
87 | {
88 | var ti = typeof(T).GetTypeInfo();
89 |
90 | if (ti.IsNullable())
91 | {
92 | // build underlying type and use wrapped formatter.
93 | ti = ti.GenericTypeArguments[0].GetTypeInfo();
94 | if (!ti.IsEnum)
95 | {
96 | return;
97 | }
98 |
99 | var innerFormatter = Instance.GetFormatterDynamic(ti.AsType());
100 | if (innerFormatter == null)
101 | {
102 | return;
103 | }
104 | formatter = (IJsonFormatter)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(ti.AsType()), new object[] { innerFormatter });
105 | return;
106 | }
107 | else if (typeof(T).IsEnum)
108 | {
109 | formatter = (IJsonFormatter)(object)new EnumFormatter(false);
110 | }
111 | }
112 | }
113 | }
114 | }
--------------------------------------------------------------------------------
/src/Utf8Json/Formatters/DynamicObjectTypeFallbackFormatter.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using System.Reflection;
4 | using System.Reflection.Emit;
5 | using Utf8Json.Internal;
6 | using Utf8Json.Internal.Emit;
7 |
8 | namespace Utf8Json.Formatters
9 | {
10 | public sealed class DynamicObjectTypeFallbackFormatter : IJsonFormatter