├── 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 11 | { 12 | delegate void SerializeMethod(object dynamicFormatter, ref JsonWriter writer, object value, IJsonFormatterResolver formatterResolver); 13 | 14 | readonly ThreadsafeTypeKeyHashTable> serializers = new ThreadsafeTypeKeyHashTable>(); 15 | 16 | readonly IJsonFormatterResolver[] innerResolvers; 17 | 18 | public DynamicObjectTypeFallbackFormatter(params IJsonFormatterResolver[] innerResolvers) 19 | { 20 | this.innerResolvers = innerResolvers; 21 | } 22 | 23 | public void Serialize(ref JsonWriter writer, object value, IJsonFormatterResolver formatterResolver) 24 | { 25 | if (value == null) { writer.WriteNull(); return; } 26 | 27 | var type = value.GetType(); 28 | 29 | if (type == typeof(object)) 30 | { 31 | // serialize to empty object 32 | writer.WriteBeginObject(); 33 | writer.WriteEndObject(); 34 | return; 35 | } 36 | 37 | KeyValuePair formatterAndDelegate; 38 | if (!serializers.TryGetValue(type, out formatterAndDelegate)) 39 | { 40 | lock (serializers) 41 | { 42 | if (!serializers.TryGetValue(type, out formatterAndDelegate)) 43 | { 44 | object formatter = null; 45 | foreach (var innerResolver in innerResolvers) 46 | { 47 | formatter = innerResolver.GetFormatterDynamic(type); 48 | if (formatter != null) break; 49 | } 50 | if (formatter == null) 51 | { 52 | throw new FormatterNotRegisteredException(type.FullName + " is not registered in this resolver. resolvers:" + string.Join(", ", innerResolvers.Select(x => x.GetType().Name).ToArray())); 53 | } 54 | 55 | var t = type; 56 | { 57 | var dm = new DynamicMethod("Serialize", null, new[] { typeof(object), typeof(JsonWriter).MakeByRefType(), typeof(object), typeof(IJsonFormatterResolver) }, type.Module, true); 58 | var il = dm.GetILGenerator(); 59 | 60 | // delegate void SerializeMethod(object dynamicFormatter, ref JsonWriter writer, object value, IJsonFormatterResolver formatterResolver); 61 | 62 | il.EmitLdarg(0); 63 | il.Emit(OpCodes.Castclass, typeof(IJsonFormatter<>).MakeGenericType(t)); 64 | il.EmitLdarg(1); 65 | il.EmitLdarg(2); 66 | il.EmitUnboxOrCast(t); 67 | il.EmitLdarg(3); 68 | 69 | il.EmitCall(Resolvers.Internal.DynamicObjectTypeBuilder.EmitInfo.Serialize(t)); 70 | 71 | il.Emit(OpCodes.Ret); 72 | 73 | formatterAndDelegate = new KeyValuePair(formatter, (SerializeMethod)dm.CreateDelegate(typeof(SerializeMethod))); 74 | } 75 | 76 | serializers.TryAdd(t, formatterAndDelegate); 77 | } 78 | } 79 | } 80 | 81 | formatterAndDelegate.Value(formatterAndDelegate.Key, ref writer, value, formatterResolver); 82 | } 83 | 84 | public object Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) 85 | { 86 | return PrimitiveObjectFormatter.Default.Deserialize(ref reader, formatterResolver); 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /src/Utf8Json.UnityClient/Assets/Scripts/Tests/SimpleTest.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 TestObject 14 | //{ 15 | // public class PrimitiveObject 16 | // { 17 | // public int v_int; 18 | 19 | // public string v_str; 20 | 21 | // public float v_float; 22 | 23 | // public bool v_bool; 24 | // public PrimitiveObject(int vi, string vs, float vf, bool vb) 25 | // { 26 | // v_int = vi; v_str = vs; v_float = vf; v_bool = vb; 27 | // } 28 | // } 29 | 30 | // [Key(0)] 31 | // public PrimitiveObject[] objectArray; 32 | 33 | // [Key(1)] 34 | // public List objectList; 35 | 36 | // [Key(2)] 37 | // public Dictionary objectMap; 38 | 39 | // public void CreateArray(int num) 40 | // { 41 | // objectArray = new PrimitiveObject[num]; 42 | // for (int i = 0; i < num; i++) 43 | // { 44 | // objectArray[i] = new PrimitiveObject(i, i.ToString(), (float)i, i % 2 == 0 ? true : false); 45 | // } 46 | // } 47 | 48 | // public void CreateList(int num) 49 | // { 50 | // objectList = new List(num); 51 | // for (int i = 0; i < num; i++) 52 | // { 53 | // objectList.Add(new PrimitiveObject(i, i.ToString(), (float)i, i % 2 == 0 ? true : false)); 54 | // } 55 | // } 56 | 57 | // public void CreateMap(int num) 58 | // { 59 | // objectMap = new Dictionary(num); 60 | // for (int i = 0; i < num; i++) 61 | // { 62 | // objectMap.Add(i.ToString(), new PrimitiveObject(i, i.ToString(), (float)i, i % 2 == 0 ? true : false)); 63 | // } 64 | // } 65 | // // I only tested with array 66 | // public static TestObject TestBuild() 67 | // { 68 | // TestObject to = new TestObject(); 69 | // //to.CreateArray(1000000); 70 | // to.CreateArray(1); 71 | 72 | // return to; 73 | // } 74 | //} 75 | 76 | //public class NonSerializableObject 77 | //{ 78 | // public int v_int; 79 | 80 | // public string v_str; 81 | 82 | // public float v_float; 83 | 84 | // public bool v_bool; 85 | // public NonSerializableObject(int vi, string vs, float vf, bool vb) 86 | // { 87 | // v_int = vi; v_str = vs; v_float = vf; v_bool = vb; 88 | // } 89 | //} 90 | 91 | [Serializable] 92 | public class Person 93 | { 94 | public int Age; 95 | public string Name; 96 | } 97 | 98 | [Serializable] 99 | public class ArrayWrapper 100 | { 101 | public Person[] wrap; 102 | } 103 | 104 | public class SimpleTest 105 | { 106 | ArrayWrapper p = new ArrayWrapper { wrap = Enumerable.Range(1, 100).Select(x => new Person { Age = x, Name = "foobar" }).ToArray() }; 107 | byte[] js = JsonSerializer.Serialize(new ArrayWrapper { wrap = Enumerable.Range(1, 100).Select(x => new Person { Age = x, Name = "foobar" }).ToArray() }); 108 | 109 | public void Utf8Json() 110 | { 111 | JsonSerializer.Serialize(p); 112 | } 113 | public void JsonUtilityToJson() 114 | { 115 | JsonUtility.ToJson(p); 116 | } 117 | public void JsonUtilityToJsonEncode() 118 | { 119 | Encoding.UTF8.GetBytes(JsonUtility.ToJson(p)); 120 | } 121 | 122 | public void Utf8JsonDeserialize() 123 | { 124 | JsonSerializer.Deserialize(js); 125 | } 126 | public void JsonUtilityFromJsonDeode() 127 | { 128 | JsonUtility.FromJson(Encoding.UTF8.GetString(js)); 129 | } 130 | } 131 | } -------------------------------------------------------------------------------- /src/Utf8Json/Formatters/TupleFormatter.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 | #if NETSTANDARD 8 | 9 | using System; 10 | using Utf8Json.Internal; 11 | using Utf8Json.Formatters.Internal; 12 | 13 | namespace Utf8Json.Formatters.Internal 14 | { 15 | // reduce static constructor generate size on generics(especially IL2CPP on Unity) 16 | internal static class TupleFormatterHelper 17 | { 18 | <# for(var i = 1; i <= 8; i++) { #> 19 | internal static readonly byte[][] nameCache<#= i #>; 20 | internal static readonly AutomataDictionary dictionary<#= i #>; 21 | <# } #> 22 | 23 | static TupleFormatterHelper() 24 | { 25 | <# for(var i = 1; i <= 8; i++) { Func toItem = x => ((x == 8) ? "Rest" : "Item" + x); #> 26 | nameCache<#= i #> = new byte[][] 27 | { 28 | <# for (var j = 1; j <= i; j++) { if (j == 1) { #> 29 | JsonWriter.GetEncodedPropertyNameWithBeginObject("<#= toItem(j) #>"), 30 | <# } else if (j == i) { #> 31 | JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("<#= toItem(j) #>"), 32 | <# } else { #> 33 | JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("<#= toItem(j) #>"), 34 | <# } } #> 35 | }; 36 | dictionary<#= i #> = new AutomataDictionary 37 | { 38 | <# for (var j = 1; j <= i; j++) { #> 39 | {JsonWriter.GetEncodedPropertyNameWithoutQuotation("<#= toItem(j) #>"), <#= j - 1 #> }, 40 | <# } #> 41 | }; 42 | <# } #> 43 | } 44 | } 45 | } 46 | 47 | namespace Utf8Json.Formatters 48 | { 49 | <# for(var i = 1; i <= 8; i++) { 50 | Func toT = x => "T" + ((x == 8) ? "Rest" : x.ToString()); 51 | Func toItem = x => ((x == 8) ? "Rest" : "Item" + x); 52 | var ts = string.Join(", ", Enumerable.Range(1, i).Select(x => toT(x))); 53 | var t = "Tuple<" + ts + ">"; 54 | #> 55 | 56 | public sealed class TupleFormatter<<#= ts #>> : IJsonFormatter<<#= t #>> 57 | { 58 | static readonly byte[][] cache = TupleFormatterHelper.nameCache<#= i #>; 59 | static readonly AutomataDictionary dictionary = TupleFormatterHelper.dictionary<#= i #>; 60 | 61 | public void Serialize(ref JsonWriter writer, <#= t #> value, IJsonFormatterResolver formatterResolver) 62 | { 63 | if (value == null) { writer.WriteNull(); return; } 64 | 65 | <# for(var j = 1; j <= i; j++) { #> 66 | writer.WriteRaw(cache[<#= j - 1 #>]); 67 | formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Serialize(ref writer, value.<#= toItem(j) #>, formatterResolver); 68 | <# } #> 69 | writer.WriteEndObject(); 70 | } 71 | 72 | public <#= t #> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) 73 | { 74 | if (reader.ReadIsNull()) return null; 75 | 76 | <# for(var j = 1; j <= i; j++) { #> 77 | <#= toT(j) #> item<#= j #> = default(<#= toT(j) #>); 78 | <# } #> 79 | 80 | var count = 0; 81 | reader.ReadIsBeginObjectWithVerify(); 82 | while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref count)) 83 | { 84 | var keyString = reader.ReadPropertyNameSegmentRaw(); 85 | int key; 86 | #if NETSTANDARD 87 | dictionary.TryGetValue(keyString, out key); 88 | #else 89 | dictionary.TryGetValueSafe(keyString, out key); 90 | #endif 91 | 92 | switch (key) 93 | { 94 | <# for(var j = 1; j <= i; j++) { #> 95 | case <#= j - 1 #>: 96 | item<#= j #> = formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(ref reader, formatterResolver); 97 | break; 98 | <# } #> 99 | default: 100 | reader.ReadNextBlock(); 101 | break; 102 | } 103 | } 104 | 105 | return new Tuple<<#= ts #>>(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => "item" + x)) #>); 106 | } 107 | } 108 | 109 | <# } #> 110 | } 111 | 112 | #endif -------------------------------------------------------------------------------- /tests/Utf8Json.Tests/NonGenericCollectionTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | using Xunit; 7 | using Utf8Json.Resolvers; 8 | 9 | namespace Utf8Json.Tests 10 | { 11 | public class NonGenericCollectionTest 12 | { 13 | [Fact] 14 | public void List() 15 | { 16 | var xs = new System.Collections.ArrayList { 1, 100, "hoge", 999.888 }; 17 | { 18 | var bin = JsonSerializer.Serialize(xs); 19 | var v = JsonSerializer.Deserialize(bin); 20 | 21 | ((double)v[0]).Is((double)1); 22 | ((double)v[1]).Is((double)100); 23 | ((string)v[2]).Is("hoge"); 24 | ((double)v[3]).Is(999.888); 25 | } 26 | { 27 | var bin = JsonSerializer.Serialize(xs); 28 | var v = JsonSerializer.Deserialize(bin); 29 | 30 | ((double)v[0]).Is((double)1); 31 | ((double)v[1]).Is((double)100); 32 | ((string)v[2]).Is("hoge"); 33 | ((double)v[3]).Is(999.888); 34 | } 35 | } 36 | 37 | [Fact] 38 | public void Dictionary() 39 | { 40 | { 41 | var xs = new System.Collections.Hashtable { { "a", 1 }, { 100, "hoge" }, { "foo", 999.888 } }; 42 | var bin = JsonSerializer.Serialize(xs); 43 | var v = JsonSerializer.Deserialize(bin); 44 | 45 | v["a"].Is((object)(double)1); 46 | v["100"].Is((object)(string)"hoge"); 47 | v["foo"].Is((object)(double)999.888); 48 | } 49 | { 50 | var xs = new System.Collections.Hashtable { { "a", 1 }, { 100, "hoge" }, { "foo", 999.888 } }; 51 | var bin = JsonSerializer.Serialize(xs); 52 | var v = JsonSerializer.Deserialize(bin); 53 | 54 | v["a"].Is((object)(double)1); 55 | v["100"].Is((object)(string)"hoge"); 56 | v["foo"].Is((object)(double)999.888); 57 | } 58 | } 59 | 60 | public void IEnumerableTest() 61 | { 62 | var xs = new System.Collections.ArrayList { 1, 100, "hoge", 999.888 }; 63 | { 64 | var bin = JsonSerializer.Serialize(xs); 65 | var v = JsonSerializer.Deserialize(bin).GetEnumerator(); 66 | 67 | v.MoveNext(); 68 | ((double)v.Current).Is((double)1); 69 | 70 | v.MoveNext(); 71 | ((double)v.Current).Is((double)100); 72 | 73 | v.MoveNext(); 74 | ((string)v.Current).Is("hoge"); 75 | 76 | v.MoveNext(); 77 | ((double)v.Current).Is(999.888); 78 | } 79 | } 80 | 81 | public void ICollectionTest() 82 | { 83 | var xs = new System.Collections.ArrayList { 1, 100, "hoge", 999.888 }; 84 | { 85 | var bin = JsonSerializer.Serialize(xs); 86 | var v2 = JsonSerializer.Deserialize(bin); 87 | v2.Count.Is(4); 88 | 89 | var v = v2.GetEnumerator(); 90 | 91 | v.MoveNext(); 92 | ((double)v.Current).Is((double)1); 93 | 94 | v.MoveNext(); 95 | ((double)v.Current).Is((double)100); 96 | 97 | v.MoveNext(); 98 | ((string)v.Current).Is("hoge"); 99 | 100 | v.MoveNext(); 101 | ((double)v.Current).Is(999.888); 102 | } 103 | } 104 | public class Wrap 105 | { 106 | public IEnumerable Seq; 107 | } 108 | 109 | [Fact] 110 | public void EnumerableNonGenericTest() 111 | { 112 | var xs = new[] { 100, 200 }; 113 | var xss = xs.Select(x => x); 114 | 115 | JsonSerializer.NonGeneric.ToJsonString(xss, StandardResolver.Default).Is("[100,200]"); 116 | JsonSerializer.NonGeneric.ToJsonString(xss, StandardResolver.AllowPrivate).Is("[100,200]"); 117 | 118 | JsonSerializer.NonGeneric.ToJsonString(new Wrap { Seq = xss }).Is("{\"Seq\":[100,200]}"); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/Utf8Json.UnityShims/Shims.cs: -------------------------------------------------------------------------------- 1 | 2 | using System.Runtime.Serialization; 3 | using Utf8Json; 4 | 5 | namespace UnityEngine 6 | { 7 | 8 | public struct Vector2 9 | { 10 | 11 | public float x; 12 | 13 | public float y; 14 | 15 | [SerializationConstructor] 16 | public Vector2(float x, float y) 17 | { 18 | this.x = x; 19 | this.y = y; 20 | } 21 | } 22 | 23 | 24 | public struct Vector3 25 | { 26 | 27 | public float x; 28 | 29 | public float y; 30 | 31 | public float z; 32 | 33 | [SerializationConstructor] 34 | public Vector3(float x, float y, float z) 35 | { 36 | this.x = x; 37 | this.y = y; 38 | this.z = z; 39 | } 40 | 41 | public static Vector3 operator *(Vector3 a, float d) 42 | { 43 | return new Vector3(a.x * d, a.y * d, a.z * d); 44 | } 45 | } 46 | 47 | 48 | public struct Vector4 49 | { 50 | 51 | public float x; 52 | 53 | public float y; 54 | 55 | public float z; 56 | 57 | public float w; 58 | 59 | [SerializationConstructor] 60 | public Vector4(float x, float y, float z, float w) 61 | { 62 | this.x = x; 63 | this.y = y; 64 | this.z = z; 65 | this.w = w; 66 | } 67 | } 68 | 69 | 70 | public struct Quaternion 71 | { 72 | 73 | public float x; 74 | 75 | public float y; 76 | 77 | public float z; 78 | 79 | public float w; 80 | 81 | [SerializationConstructor] 82 | public Quaternion(float x, float y, float z, float w) 83 | { 84 | this.x = x; 85 | this.y = y; 86 | this.z = z; 87 | this.w = w; 88 | } 89 | } 90 | 91 | 92 | public struct Color 93 | { 94 | 95 | public float r; 96 | 97 | public float g; 98 | 99 | public float b; 100 | 101 | public float a; 102 | 103 | public Color(float r, float g, float b) 104 | : this(r, g, b, 1.0f) 105 | { 106 | 107 | } 108 | 109 | [SerializationConstructor] 110 | public Color(float r, float g, float b, float a) 111 | { 112 | this.r = r; 113 | this.g = g; 114 | this.b = b; 115 | this.a = a; 116 | } 117 | } 118 | 119 | 120 | public struct Bounds 121 | { 122 | 123 | public Vector3 center { get; set; } 124 | 125 | [IgnoreDataMember] 126 | public Vector3 extents { get; set; } 127 | 128 | 129 | public Vector3 size 130 | { 131 | get 132 | { 133 | return this.extents * 2f; 134 | } 135 | set 136 | { 137 | this.extents = value * 0.5f; 138 | } 139 | } 140 | 141 | [SerializationConstructor] 142 | public Bounds(Vector3 center, Vector3 size) 143 | { 144 | this.center = center; 145 | this.extents = size * 0.5f; 146 | } 147 | } 148 | 149 | 150 | public struct Rect 151 | { 152 | 153 | public float x { get; set; } 154 | 155 | 156 | public float y { get; set; } 157 | 158 | 159 | public float width { get; set; } 160 | 161 | 162 | public float height { get; set; } 163 | 164 | [SerializationConstructor] 165 | public Rect(float x, float y, float width, float height) 166 | { 167 | this.x = x; 168 | this.y = y; 169 | this.width = width; 170 | this.height = height; 171 | } 172 | 173 | public Rect(Vector2 position, Vector2 size) 174 | { 175 | this.x = position.x; 176 | this.y = position.y; 177 | this.width = size.x; 178 | this.height = size.y; 179 | } 180 | 181 | public Rect(Rect source) 182 | { 183 | this.x = source.x; 184 | this.y = source.y; 185 | this.width = source.width; 186 | this.height = source.height; 187 | } 188 | } 189 | } -------------------------------------------------------------------------------- /src/Utf8Json/Internal/DoubleConversion/DiyFp.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Utf8Json.Internal.DoubleConversion 6 | { 7 | // https://github.com/google/double-conversion/blob/master/double-conversion/diy-fp.cc 8 | // https://github.com/google/double-conversion/blob/master/double-conversion/diy-fp.h 9 | 10 | internal struct DiyFp 11 | { 12 | public const int kSignificandSize = 64; 13 | public const ulong kUint64MSB = 0x8000000000000000; // 0x80000000_00000000; 14 | 15 | // uint64_t f_; 16 | // int e_; 17 | // long f() const { return f_; } 18 | // int e() const { return e_; } 19 | // void set_f(long new_value) { f_ = new_value; } 20 | // void set_e(int new_value) { e_ = new_value; } 21 | 22 | // public field, not safe... 23 | public ulong f; 24 | public int e; 25 | 26 | public DiyFp(ulong significand, int exponent) 27 | { 28 | this.f = significand; 29 | this.e = exponent; 30 | } 31 | 32 | // this = this - other. 33 | // The exponents of both numbers must be the same and the significand of this 34 | // must be bigger than the significand of other. 35 | // The result will not be normalized. 36 | public void Subtract(ref DiyFp other) 37 | { 38 | f -= other.f; 39 | } 40 | 41 | // Returns a - b. 42 | // The exponents of both numbers must be the same and this must be bigger 43 | // than other. The result will not be normalized. 44 | public static DiyFp Minus(ref DiyFp a, ref DiyFp b) 45 | { 46 | DiyFp result = a; 47 | result.Subtract(ref b); 48 | return result; 49 | } 50 | 51 | public static DiyFp operator -(DiyFp lhs, DiyFp rhs) 52 | { 53 | return Minus(ref lhs, ref rhs); 54 | } 55 | 56 | // this = this * other. 57 | public void Multiply(ref DiyFp other) 58 | { 59 | // Simply "emulates" a 128 bit multiplication. 60 | // However: the resulting number only contains 64 bits. The least 61 | // significant 64 bits are only used for rounding the most significant 64 62 | // bits. 63 | const long kM32 = 0xFFFFFFFFU; 64 | ulong a = f >> 32; 65 | ulong b = f & kM32; 66 | ulong c = other.f >> 32; 67 | ulong d = other.f & kM32; 68 | ulong ac = a * c; 69 | ulong bc = b * c; 70 | ulong ad = a * d; 71 | ulong bd = b * d; 72 | ulong tmp = (bd >> 32) + (ad & kM32) + (bc & kM32); 73 | // By adding 1U << 31 to tmp we round the final result. 74 | // Halfway cases will be round up. 75 | tmp += 1U << 31; 76 | ulong result_f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32); 77 | e += other.e + 64; 78 | f = result_f; 79 | } 80 | 81 | // returns a * b; 82 | public static DiyFp Times(ref DiyFp a, ref DiyFp b) 83 | { 84 | DiyFp result = a; 85 | result.Multiply(ref b); 86 | return result; 87 | } 88 | 89 | public static DiyFp operator *(DiyFp lhs, DiyFp rhs) 90 | { 91 | return Times(ref lhs, ref rhs); 92 | } 93 | 94 | public void Normalize() 95 | { 96 | ulong significand = f; 97 | int exponent = e; 98 | 99 | // This method is mainly called for normalizing boundaries. In general 100 | // boundaries need to be shifted by 10 bits. We thus optimize for this case. 101 | const ulong k10MSBits = 0xFFC0000000000000; // UINT64_2PART_C(0xFFC00000, 00000000); 102 | while ((significand & k10MSBits) == 0) 103 | { 104 | significand <<= 10; 105 | exponent -= 10; 106 | } 107 | while ((significand & kUint64MSB) == 0) 108 | { 109 | significand <<= 1; 110 | exponent--; 111 | } 112 | f = significand; 113 | e = exponent; 114 | } 115 | 116 | public static DiyFp Normalize(ref DiyFp a) 117 | { 118 | DiyFp result = a; 119 | result.Normalize(); 120 | return result; 121 | } 122 | } 123 | } -------------------------------------------------------------------------------- /src/Utf8Json/Formatters/ValueTupleFormatter.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 | #if NETSTANDARD 8 | 9 | using System; 10 | using Utf8Json.Internal; 11 | using Utf8Json.Formatters.Internal; 12 | 13 | namespace Utf8Json.Formatters.Internal 14 | { 15 | // reduce static constructor generate size on generics(especially IL2CPP on Unity) 16 | internal static class ValueTupleFormatterHelper 17 | { 18 | <# for(var i = 1; i <= 8; i++) { #> 19 | internal static readonly byte[][] nameCache<#= i #>; 20 | internal static readonly AutomataDictionary dictionary<#= i #>; 21 | <# } #> 22 | 23 | static ValueTupleFormatterHelper() 24 | { 25 | <# for(var i = 1; i <= 8; i++) { Func toItem = x => ((x == 8) ? "Rest" : "Item" + x); #> 26 | nameCache<#= i #> = new byte[][] 27 | { 28 | <# for (var j = 1; j <= i; j++) { if (j == 1) { #> 29 | JsonWriter.GetEncodedPropertyNameWithBeginObject("<#= toItem(j) #>"), 30 | <# } else if (j == i) { #> 31 | JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("<#= toItem(j) #>"), 32 | <# } else { #> 33 | JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("<#= toItem(j) #>"), 34 | <# } } #> 35 | }; 36 | dictionary<#= i #> = new AutomataDictionary 37 | { 38 | <# for (var j = 1; j <= i; j++) { #> 39 | {JsonWriter.GetEncodedPropertyNameWithoutQuotation("<#= toItem(j) #>"), <#= j - 1 #> }, 40 | <# } #> 41 | }; 42 | <# } #> 43 | } 44 | } 45 | } 46 | 47 | namespace Utf8Json.Formatters 48 | { 49 | <# for(var i = 1; i <= 8; i++) { 50 | Func toT = x => "T" + ((x == 8) ? "Rest" : x.ToString()); 51 | Func toItem = x => ((x == 8) ? "Rest" : "Item" + x); 52 | var ts = string.Join(", ", Enumerable.Range(1, i).Select(x => toT(x))); 53 | var t = "ValueTuple<" + ts + ">"; 54 | #> 55 | 56 | public sealed class ValueTupleFormatter<<#= ts #>> : IJsonFormatter<<#= t #>><#= (t.Contains("TRest") ? " where TRest : struct" : "") #> 57 | { 58 | static readonly byte[][] cache = TupleFormatterHelper.nameCache<#= i #>; 59 | static readonly AutomataDictionary dictionary = TupleFormatterHelper.dictionary<#= i #>; 60 | 61 | public void Serialize(ref JsonWriter writer, <#= t #> value, IJsonFormatterResolver formatterResolver) 62 | { 63 | <# for(var j = 1; j <= i; j++) { #> 64 | writer.WriteRaw(cache[<#= j - 1 #>]); 65 | formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Serialize(ref writer, value.<#= toItem(j) #>, formatterResolver); 66 | <# } #> 67 | writer.WriteEndObject(); 68 | } 69 | 70 | public <#= t #> Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) 71 | { 72 | if (reader.ReadIsNull()) throw new InvalidOperationException("Data is Nil, ValueTuple can not be null."); 73 | 74 | <# for(var j = 1; j <= i; j++) { #> 75 | <#= toT(j) #> item<#= j #> = default(<#= toT(j) #>); 76 | <# } #> 77 | 78 | var count = 0; 79 | reader.ReadIsBeginObjectWithVerify(); 80 | while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref count)) 81 | { 82 | var keyString = reader.ReadPropertyNameSegmentRaw(); 83 | int key; 84 | #if NETSTANDARD 85 | dictionary.TryGetValue(keyString, out key); 86 | #else 87 | dictionary.TryGetValueSafe(keyString, out key); 88 | #endif 89 | 90 | switch (key) 91 | { 92 | <# for(var j = 1; j <= i; j++) { #> 93 | case <#= j - 1 #>: 94 | item<#= j #> = formatterResolver.GetFormatterWithVerify<<#= toT(j) #>>().Deserialize(ref reader, formatterResolver); 95 | break; 96 | <# } #> 97 | default: 98 | reader.ReadNextBlock(); 99 | break; 100 | } 101 | } 102 | 103 | return new ValueTuple<<#= ts #>>(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => "item" + x)) #>); 104 | } 105 | } 106 | 107 | <# } #> 108 | } 109 | 110 | #endif -------------------------------------------------------------------------------- /src/Utf8Json.CodeGenerator/packages.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 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /tests/Utf8Json.Tests/ReadNextBlockSegmentTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Utf8Json.Internal; 3 | using Xunit; 4 | 5 | namespace Utf8Json.Tests 6 | { 7 | public class ReadNextBlockSegmentTest 8 | { 9 | [JsonFormatter(typeof(ContainerFormatter))] 10 | public class Container 11 | { 12 | public string Type { get; set; } 13 | public IValue Value { get; set; } 14 | 15 | public class ContainerFormatter : IJsonFormatter 16 | { 17 | static readonly AutomataDictionary automata = new AutomataDictionary(); 18 | 19 | static ContainerFormatter() 20 | { 21 | automata.Add("Type", 0); 22 | automata.Add("Value", 1); 23 | } 24 | 25 | 26 | public Container Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) 27 | { 28 | if (reader.ReadIsNull()) return null; 29 | 30 | string type = null; 31 | ArraySegment valueSegment = default(ArraySegment); 32 | 33 | var count = 0; 34 | while (reader.ReadIsInObject(ref count)) 35 | { 36 | var propName = reader.ReadPropertyNameSegmentRaw(); 37 | var i = -1; 38 | automata.TryGetValue(propName, out i); 39 | switch (i) 40 | { 41 | case 0: 42 | type = reader.ReadString(); 43 | break; 44 | case 1: 45 | valueSegment = reader.ReadNextBlockSegment(); 46 | break; 47 | default: 48 | reader.ReadNextBlock(); 49 | break; 50 | } 51 | } 52 | 53 | var result = new Container { Type = type }; 54 | 55 | switch (type) 56 | { 57 | case "TypeA": 58 | { 59 | var childReader = new JsonReader(valueSegment.Array, valueSegment.Offset); 60 | result.Value = formatterResolver.GetFormatterWithVerify().Deserialize(ref childReader, formatterResolver); 61 | } 62 | break; 63 | case "TypeB": 64 | { 65 | var childReader = new JsonReader(valueSegment.Array, valueSegment.Offset); 66 | result.Value = formatterResolver.GetFormatterWithVerify().Deserialize(ref childReader, formatterResolver); 67 | } 68 | break; 69 | default: 70 | break; 71 | } 72 | 73 | return result; 74 | } 75 | 76 | public void Serialize(ref JsonWriter writer, Container value, IJsonFormatterResolver formatterResolver) 77 | { 78 | throw new NotImplementedException(); 79 | } 80 | } 81 | } 82 | 83 | public interface IValue 84 | { 85 | } 86 | 87 | public class ValueTypeA : IValue 88 | { 89 | public int Foo { get; set; } 90 | } 91 | public class ValueTypeB : IValue 92 | { 93 | public string Hoge { get; set; } 94 | } 95 | 96 | [Fact] 97 | public void Test() 98 | { 99 | var json = @" 100 | { 101 | ""Type"": ""TypeA"", 102 | ""Value"": { 103 | ""Foo"": 10 104 | } 105 | } 106 | "; 107 | 108 | var container = JsonSerializer.Deserialize(json); 109 | container.Type.Is("TypeA"); 110 | container.Value.IsInstanceOf().Foo.Is(10); 111 | 112 | json = @" 113 | { 114 | ""Value"": { 115 | ""Hoge"": ""mogemoge"" 116 | }, 117 | ""Type"": ""TypeB"" 118 | } 119 | "; 120 | 121 | container = JsonSerializer.Deserialize(json); 122 | container.Type.Is("TypeB"); 123 | container.Value.IsInstanceOf().Hoge.Is("mogemoge"); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/Utf8Json.CodeGenerator/Generator/FormatterTemplate.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 | #pragma warning disable 618 7 | #pragma warning disable 612 8 | #pragma warning disable 414 9 | #pragma warning disable 219 10 | #pragma warning disable 168 11 | 12 | namespace <#= Namespace #> 13 | { 14 | using System; 15 | using Utf8Json; 16 | 17 | <# foreach(var objInfo in objectSerializationInfos) { #> 18 | 19 | public sealed class <#= objInfo.Name #>Formatter : global::Utf8Json.IJsonFormatter<<#= objInfo.FullName #>> 20 | { 21 | readonly global::Utf8Json.Internal.AutomataDictionary ____keyMapping; 22 | readonly byte[][] ____stringByteKeys; 23 | 24 | public <#= objInfo.Name #>Formatter() 25 | { 26 | this.____keyMapping = new global::Utf8Json.Internal.AutomataDictionary() 27 | { 28 | <# var index = 0; foreach(var x in objInfo.Members) { #> 29 | { JsonWriter.GetEncodedPropertyNameWithoutQuotation("<#= x.Name #>"), <#= index++ #>}, 30 | <# } #> 31 | }; 32 | 33 | this.____stringByteKeys = new byte[][] 34 | { 35 | <# index = 0; foreach(var x in objInfo.Members.Where(x => x.IsReadable)) { #> 36 | <# if(index++ == 0) { #> 37 | JsonWriter.GetEncodedPropertyNameWithBeginObject("<#= x.Name #>"), 38 | <# } else { #> 39 | JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator("<#= x.Name #>"), 40 | <# }} #> 41 | }; 42 | } 43 | 44 | public void Serialize(ref JsonWriter writer, <#= objInfo.FullName #> value, global::Utf8Json.IJsonFormatterResolver formatterResolver) 45 | { 46 | <# if( objInfo.IsClass) { #> 47 | if (value == null) 48 | { 49 | writer.WriteNull(); 50 | return; 51 | } 52 | <# } #> 53 | 54 | <# index = 0; foreach(var x in objInfo.Members.Where(x=>x.IsReadable)) { #> 55 | writer.WriteRaw(this.____stringByteKeys[<#= index++ #>]); 56 | <#= x.GetSerializeMethodString() #>; 57 | <# } #> 58 | 59 | writer.WriteEndObject(); 60 | } 61 | 62 | public <#= objInfo.FullName #> Deserialize(ref JsonReader reader, global::Utf8Json.IJsonFormatterResolver formatterResolver) 63 | { 64 | if (reader.ReadIsNull()) 65 | { 66 | <# if( objInfo.IsClass) { #> 67 | return null; 68 | <# } else { #> 69 | throw new InvalidOperationException("typecode is null, struct not supported"); 70 | <# } #> 71 | } 72 | 73 | <# if( !objInfo.HasConstructor) { #> 74 | throw new InvalidOperationException("generated serializer for IInterface does not support deserialize."); 75 | <# } else { #> 76 | 77 | <# foreach(var x in objInfo.Members) { #> 78 | var __<#= x.MemberName #>__ = default(<#= x.Type #>); 79 | var __<#= x.MemberName #>__b__ = false; 80 | <# } #> 81 | 82 | var ____count = 0; 83 | reader.ReadIsBeginObjectWithVerify(); 84 | while (!reader.ReadIsEndObjectWithSkipValueSeparator(ref ____count)) 85 | { 86 | var stringKey = reader.ReadPropertyNameSegmentRaw(); 87 | int key; 88 | if (!____keyMapping.TryGetValueSafe(stringKey, out key)) 89 | { 90 | reader.ReadNextBlock(); 91 | goto NEXT_LOOP; 92 | } 93 | 94 | switch (key) 95 | { 96 | <# index = 0; foreach(var x in objInfo.Members) { #> 97 | case <#= index++ #>: 98 | __<#= x.MemberName #>__ = <#= x.GetDeserializeMethodString() #>; 99 | __<#= x.MemberName #>__b__ = true; 100 | break; 101 | <# } #> 102 | default: 103 | reader.ReadNextBlock(); 104 | break; 105 | } 106 | 107 | NEXT_LOOP: 108 | continue; 109 | } 110 | 111 | var ____result = new <#= objInfo.GetConstructorString() #>; 112 | <# foreach(var x in objInfo.Members.Where(x => x.IsWritable)) { #> 113 | if(__<#= x.MemberName #>__b__) ____result.<#= x.MemberName #> = __<#= x.MemberName #>__; 114 | <# } #> 115 | 116 | return ____result; 117 | <# } #> 118 | } 119 | } 120 | 121 | <# } #> 122 | } 123 | 124 | #pragma warning disable 168 125 | #pragma warning restore 219 126 | #pragma warning restore 414 127 | #pragma warning restore 618 128 | #pragma warning restore 612 -------------------------------------------------------------------------------- /tests/Utf8Json.Tests/DoubleConversionTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | using Utf8Json.Internal.DoubleConversion; 6 | using Xunit; 7 | using Utf8Json.Internal; 8 | 9 | namespace Utf8Json.Tests 10 | { 11 | public class DoubleConversionTest 12 | { 13 | public static bool Approximately(float a, float b) 14 | { 15 | return Math.Abs(b - a) < Math.Max(1E-06f * Math.Max(Math.Abs(a), Math.Abs(b)), float.Epsilon * 8f); 16 | } 17 | public static bool Approximately(double a, double b) 18 | { 19 | return Math.Abs(b - a) < Math.Max(1E-06 * Math.Max(Math.Abs(a), Math.Abs(b)), double.Epsilon * 8.0); 20 | } 21 | 22 | static string GetString(double v) 23 | { 24 | byte[] buf = null; 25 | var len = NumberConverter.WriteDouble(ref buf, 0, v); 26 | return Encoding.UTF8.GetString(buf, 0, len); 27 | } 28 | 29 | static string GetString(float v) 30 | { 31 | byte[] buf = null; 32 | var len = NumberConverter.WriteSingle(ref buf, 0, v); 33 | return Encoding.UTF8.GetString(buf, 0, len); 34 | } 35 | 36 | [Fact] 37 | public void Double() 38 | { 39 | // testdatagen, https://github.com/ufcpp/UfcppSample/blob/master/Demo/2017/TypeRepositoryBenchmarks/Grisu3DoubleConversion/TestData.cs 40 | 41 | var r = new Random(); 42 | var n = 10000; 43 | 44 | var x = new double[3 * n + 1]; 45 | x[0] = 2e15; 46 | var i = 1; 47 | for (; i <= n; i++) x[i] = r.Next() * Math.Pow(10, r.Next(1, 15)); 48 | for (; i <= 2 * n; i++) x[i] = (2 * r.NextDouble() - 1) * Math.Pow(10, r.Next(-300, 300)); 49 | for (; i <= 3 * n; i++) x[i] = (2 * r.NextDouble() - 1) * Math.Pow(10, r.Next(-5, 15)); 50 | 51 | foreach (var item in x.Concat(new[] { double.Epsilon, double.MaxValue, double.MinValue })) 52 | { 53 | var actual = GetString(item); 54 | var y = double.Parse(actual); 55 | var diff = Math.Abs((item - y) / item); 56 | if (diff > 1E-15) throw new Exception(item + " : " + diff.ToString()); 57 | 58 | if (!(item == double.MaxValue || item == double.MinValue)) 59 | { 60 | var buf = Encoding.UTF8.GetBytes(item.ToString()); 61 | var buf2 = Enumerable.Range(1, 100).Select(z => (byte)z).Concat(buf).ToArray(); 62 | var d2 = NumberConverter.ReadDouble(buf2, 100, out var _); 63 | Approximately(y, d2).IsTrue(); 64 | } 65 | } 66 | 67 | // same 68 | foreach (var item in new[] { double.NaN, double.NegativeInfinity, double.PositiveInfinity, 0.000, }) 69 | { 70 | GetString(item).Is(item.ToString()); 71 | } 72 | 73 | // has e 74 | foreach (var item in new[] { 1000000000000000.0, 0.00001 }) 75 | { 76 | GetString(item).Contains("E").IsTrue(); 77 | } 78 | } 79 | 80 | [Fact] 81 | public void Float() 82 | { 83 | var r = new Random(); 84 | var n = 10000; 85 | 86 | var y = new float[3 * n + 1]; 87 | y[0] = 2e7f; 88 | var i = 1; 89 | for (; i <= n; i++) y[i] = (float)(r.Next() * Math.Pow(10, r.Next(1, 7))); 90 | for (; i <= 2 * n; i++) y[i] = (float)((2 * r.NextDouble() - 1) * Math.Pow(10, r.Next(-35, 35))); 91 | for (; i <= 3 * n; i++) y[i] = (float)((2 * r.NextDouble() - 1) * Math.Pow(10, r.Next(-5, 7))); 92 | 93 | foreach (var item in y.Concat(new[] { float.Epsilon, float.MaxValue, float.MinValue })) 94 | { 95 | var actual = GetString(item); 96 | var y2 = float.Parse(actual); 97 | var diff = Math.Abs((item - y2) / item); 98 | if (diff > 2E-7) throw new Exception(item + " : " + diff.ToString()); 99 | 100 | var buf = Encoding.UTF8.GetBytes(item.ToString()); 101 | var buf2 = Enumerable.Range(1, 100).Select(z => (byte)z).Concat(buf).ToArray(); 102 | var d2 = NumberConverter.ReadSingle(buf2, 100, out var _); 103 | Approximately(y2, d2).IsTrue(); 104 | } 105 | 106 | // same 107 | foreach (var item in new[] { float.NaN, float.NegativeInfinity, float.PositiveInfinity, 0.000f, }) 108 | { 109 | GetString(item).Is(item.ToString()); 110 | } 111 | 112 | // has e 113 | foreach (var item in new[] { 1000000000000000.0f, 0.00001f }) 114 | { 115 | GetString(item).Contains("E").IsTrue(); 116 | } 117 | } 118 | } 119 | } 120 | --------------------------------------------------------------------------------