Skip to content

Commit

Permalink
Merge pull request #15 from DanKE123abc/dev
Browse files Browse the repository at this point in the history
v1.3.3
  • Loading branch information
DanKE123abc authored Jul 28, 2024
2 parents d073d35 + d3a077d commit 06fba82
Showing 6 changed files with 64 additions and 42 deletions.
10 changes: 5 additions & 5 deletions DanKeJson.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -39,12 +39,12 @@

string json = DanKeJson.JSON.ToJson(person);
Person newperson = DanKeJson.JSON.ToData<Person>(json);
string json5 = DanKeJson.JSON5.ToJson(newperson, new Json5Config
string json5 = DanKeJson.JSON5.ToJson(newperson, new Json5Options()
{
AddCommaForObject = false,
AddCommaForArray = false,
KeyNameStyle = Json5Config.KeyNameType.WithoutQuotes,
StringQuoteStyle = Json5Config.StringQuoteType.SingleQuote,
AddTailingCommaForObject = true,
AddTailingCommaForArray = true,
KeyNameStyle = Json5Options.KeyNameType.WithoutQuotes,
StringQuoteStyle = Json5Options.StringQuoteType.SingleQuote,
});

Console.WriteLine(json);
2 changes: 1 addition & 1 deletion DanKeJson/DanKeJson.nuspec
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<package >
<metadata>
<id>DanKeJson</id>
<version>1.3.2</version>
<version>1.3.3</version>
<title>DanKeJson</title>
<authors>DanKe</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1 change: 1 addition & 0 deletions DanKeJson/JSON.cs
Original file line number Diff line number Diff line change
@@ -160,6 +160,7 @@ private static bool IsFilePath(string path)
}
return true;
}

private static object FromJson(JsonData json, Type type)
{
object dataclass = Activator.CreateInstance(type);
61 changes: 41 additions & 20 deletions DanKeJson/JSON5.cs
Original file line number Diff line number Diff line change
@@ -34,7 +34,14 @@

namespace DanKeJson
{
public class Json5Config
//TODO:兼容旧接口,在v1.4.0移除
[Obsolete("Please use the [Json5Options] class, as it will be deprecated in v1.4.0.",false)]
public class Json5Config : Json5Options
{
}
//END

public class Json5Options
{
public enum KeyNameType
{
@@ -48,12 +55,19 @@ public enum StringQuoteType
DoubleQuote
}

[Obsolete("Please use the [AddTailingCommaForObject] property, as it will be deprecated in v1.4.0.",false)]
public bool AddCommaForObject { get; set; } = false;

[Obsolete("Please use the [AddTailingCommaForArray] property, as it will be deprecated in v1.4.0.",false)]
public bool AddCommaForArray { get; set; } = false;

public bool AddTailingCommaForObject { get; set; } = false;

public bool AddTailingCommaForArray { get; set; } = false;

public KeyNameType KeyNameStyle { get; set; } = KeyNameType.WithQuotes;
public StringQuoteType StringQuoteStyle { get; set; } = StringQuoteType.DoubleQuote;


}

/// <summary>
@@ -96,22 +110,22 @@ public static JsonData ToData(string text, bool useComments = true, bool skipFil
/// About Json5 : https://json5.org
/// </summary>
/// <param name="json">the JsonData</param>
/// <param name="config">JSON 5 format preferences</param>
/// <param name="options">JSON 5 format preferences</param>
/// <returns>Json(String)</returns>
public static string ToJson(JsonData json, Json5Config config = null)
public static string ToJson(JsonData json, Json5Options options = null)
{
if (json == null)
{
return null;
}

if (config == null)
if (options == null)
{
config = new Json5Config();
options = new Json5Options();
}

StringBuilder stringBuilder = new StringBuilder();
ProcessData(json, stringBuilder, config);
ProcessData(json, stringBuilder, options);
return stringBuilder.ToString();
}

@@ -120,40 +134,47 @@ public static string ToJson(JsonData json, Json5Config config = null)
/// About Json5 : https://json5.org
/// </summary>
/// <param name="jsonObject">object instantiated by the class</param>
/// <param name="config">JSON 5 format preferences</param>
/// <param name="options">JSON 5 format preferences</param>
/// <returns>Json(String)</returns>
public static string ToJson(object jsonObject, Json5Config config = null)
public static string ToJson(object jsonObject, Json5Options options = null)
{
if (jsonObject == null)
{
return null;
}

if (config == null)
if (options == null)
{
config = new Json5Config();
options = new Json5Options();
}

JsonData json = FromObject(jsonObject);
StringBuilder stringBuilder = new StringBuilder();
ProcessData(json, stringBuilder, config);
ProcessData(json, stringBuilder, options);
return stringBuilder.ToString();
}

private static void ProcessData(JsonData json, StringBuilder builder, Json5Config config)
private static void ProcessData(JsonData json, StringBuilder builder, Json5Options options)
{
if (json == null || builder == null)
{
return;
}

//TODO:兼容旧接口,在v1.4.0移除
if (options.AddTailingCommaForObject != options.AddCommaForObject)
options.AddTailingCommaForObject = options.AddCommaForObject;
if (options.AddTailingCommaForArray != options.AddCommaForArray)
options.AddTailingCommaForArray = options.AddCommaForArray;
//END

switch (json.type)
{
case JsonData.Type.Number:
builder.Append(json.json);
break;
case JsonData.Type.String:
switch (config.StringQuoteStyle)
switch (options.StringQuoteStyle)
{
case Json5Config.StringQuoteType.DoubleQuote:
builder.Append("\"" + json.json[1..^1] + "\"");
@@ -173,7 +194,7 @@ private static void ProcessData(JsonData json, StringBuilder builder, Json5Confi
builder.Append('{');
foreach (var key in json.map.Keys)
{
switch (config.KeyNameStyle)
switch (options.KeyNameStyle)
{
case Json5Config.KeyNameType.WithQuotes:
builder.Append("\"" + key + "\":");
@@ -182,12 +203,12 @@ private static void ProcessData(JsonData json, StringBuilder builder, Json5Confi
builder.Append(key + ":");
break;
}
ProcessData(json[key], builder, config);
ProcessData(json[key], builder, options);
builder.Append(',');
}

builder.Remove(builder.Length - 1, 1);
if (config.AddCommaForObject)
if (options.AddTailingCommaForObject)
{
builder.Append(',');
}
@@ -197,11 +218,11 @@ private static void ProcessData(JsonData json, StringBuilder builder, Json5Confi
builder.Append('[');
foreach (var item in json.array)
{
ProcessData(item, builder, config);
ProcessData(item, builder, options);
builder.Append(',');
}
builder.Remove(builder.Length - 1, 1);
if (config.AddCommaForArray)
if (options.AddTailingCommaForArray)
{
builder.Append(',');
}
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -68,46 +68,46 @@ DanKeJson 为了方便开发使用,添加了许多特性,包括但不限于

## 在你的应用中安装 DanKeJson

**[点击下载](https://www.nuget.org/api/v2/package/DanKeJson/1.3.2)**
**[点击下载](https://www.nuget.org/api/v2/package/DanKeJson/1.3.3)**

### Package manager

```shell
NuGet\Install-Package DanKeJson -Version 1.3.2
NuGet\Install-Package DanKeJson -Version 1.3.3
```

### .NET CLI

```shell
dotnet add package DanKeJson --version 1.3.2
dotnet add package DanKeJson --version 1.3.3
```

### PackageReference

```xaml
<PackageReference Include="DanKeJson" Version="1.3.2" />
<PackageReference Include="DanKeJson" Version="1.3.3" />
```

### Paket CLI

```shell
paket add DanKeJson --version 1.3.2
paket add DanKeJson --version 1.3.3
```

### Script & Interactive

```c#
#r "nuget: DanKeJson, 1.3.2"
#r "nuget: DanKeJson, 1.3.3"
```

### Cake

```C#
// Install DanKeJson as a Cake Addin
#addin nuget:?package=DanKeJson&version=1.3.2
#addin nuget:?package=DanKeJson&version=1.3.3

// Install DanKeJson as a Cake Tool
#tool nuget:?package=DanKeJson&version=1.3.2
#tool nuget:?package=DanKeJson&version=1.3.3
```

或者,只需复制目录`./publish/DanKeJson`到您自己项目的源代码树中,并将其与您的开发环境集成。
16 changes: 8 additions & 8 deletions README_en.md
Original file line number Diff line number Diff line change
@@ -68,46 +68,46 @@ Serialization:

## Using DanKeJson from an application

**[Download package](https://www.nuget.org/api/v2/package/DanKeJson/1.3.2)**
**[Download package](https://www.nuget.org/api/v2/package/DanKeJson/1.3.3)**

### Package manager

```shell
NuGet\Install-Package DanKeJson -Version 1.3.2
NuGet\Install-Package DanKeJson -Version 1.3.3
```

### .NET CLI

```shell
dotnet add package DanKeJson --version 1.3.2
dotnet add package DanKeJson --version 1.3.3
```

### PackageReference

```xaml
<PackageReference Include="DanKeJson" Version="1.3.2" />
<PackageReference Include="DanKeJson" Version="1.3.3" />
```

### Paket CLI

```shell
paket add DanKeJson --version 1.3.2
paket add DanKeJson --version 1.3.3
```

### Script & Interactive

```c#
#r "nuget: DanKeJson, 1.3.2"
#r "nuget: DanKeJson, 1.3.3"
```

### Cake

```C#
// Install DanKeJson as a Cake Addin
#addin nuget:?package=DanKeJson&version=1.3.2
#addin nuget:?package=DanKeJson&version=1.3.3

// Install DanKeJson as a Cake Tool
#tool nuget:?package=DanKeJson&version=1.3.2
#tool nuget:?package=DanKeJson&version=1.3.3
```

Alternatively, just copy the whole tree of files under `./publish/DanKeJson` to your own project's source tree and integrate it with your development environment.

0 comments on commit 06fba82

Please sign in to comment.