Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added support for arrays based on primitive types #15

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Riverside.JsonBinder.Console/InteractiveMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private static void ConvertJsonToClasses()
}
catch (Exception ex)
{
throw;
ConsoleHelpers.DisplayError("An unexpected error occurred.", ex.Message);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down
45 changes: 25 additions & 20 deletions src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Riverside.JsonBinder.Serialization;

Expand Down Expand Up @@ -33,26 +34,24 @@
var classDef = $"public class {className}\n{{";
foreach (var property in obj)
{
var propType = GetType(property.Value, ToPascalCase(property.Key));
var propType = GetType(property.Value, className + '_' + ToPascalCase(property.Key));
classDef += $"\n\n [JsonPropertyName(\"{property.Key}\")]";
classDef += $"\n public {propType} {ToPascalCase(property.Key)} {{ get; set; }}";
}
classDef += "\n}";
classes.Add(classDef);

foreach (var property in obj)
{
if (property.Value is JsonObject || property.Value is JsonArray)
if (property.Value is JsonObject)
{
ProcessNode(property.Value, ToPascalCase(property.Key), classes);
ProcessNode(property.Value, className + '_' + ToPascalCase(property.Key), classes);
}
else if (property.Value is JsonArray array && array.Count > 0 && array[0] is JsonObject)
{
// Only process arrays of objects
ProcessNode(array[0], className + '_' + ToPascalCase(property.Key + "Item"), classes);

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.

Check warning on line 53 in src/Riverside.JsonBinder/Serialization/CSharpSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Possible null reference argument for parameter 'node' in 'void CSharpSerializer.ProcessNode(JsonNode node, string className, List<string> classes)'.
}
}
}
else if (node is JsonArray array && array.Count > 0)
{
var firstElement = array[0];
if (firstElement is JsonObject || firstElement is JsonArray)
{
ProcessNode(firstElement, className + "Item", classes);
}
}
}
Expand All @@ -65,15 +64,21 @@
/// <returns>The C# type as a string.</returns>
public override string GetType(JsonNode? node, string propertyName)
{
return node switch
if (node is JsonArray array && array.Count > 0)
{
JsonObject => propertyName,
JsonArray => $"List<{propertyName}Item>",
JsonValue value when value.TryGetValue<int>(out _) => "int",
JsonValue value when value.TryGetValue<double>(out _) => "double",
JsonValue value when value.TryGetValue<string>(out _) => "string",
JsonValue value when value.TryGetValue<bool>(out _) => "bool",
_ => "object"
};
var firstElement = array[0];
if (firstElement is JsonValue val)
{
return $"List<{GetValueType(val, SerializableLanguage.CSharp)}>";
}
// Handle complex object arrays
return $"List<{propertyName}Item>";
}
else if (node is JsonObject)
return propertyName;
else if (node is JsonValue val)
return GetValueType(val, SerializableLanguage.CSharp);

return "object";
}
}
76 changes: 74 additions & 2 deletions src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Nodes;
using System.Xml.Linq;

namespace Riverside.JsonBinder.Serialization;

Expand All @@ -22,17 +23,88 @@
/// <param name="propertyName">The name of the property.</param>
/// <returns>The type as a string.</returns>
public abstract string GetType(JsonNode? node, string propertyName);
public string ToPascalCase(string input)

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'

Check warning on line 26 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.ToPascalCase(string)'
{
if (string.IsNullOrEmpty(input))
{
return input;
}

// Split the input into words using non-alphanumeric characters as delimiters.
var words = input.Split(new[] { '_', '-', ' ', '.' }, StringSplitOptions.RemoveEmptyEntries);
// Use a regular expression to split by non-alphanumeric delimiters and case transitions.
var words = System.Text.RegularExpressions.Regex
.Split(input, @"(?<!^)(?=[A-Z])|[_\-\.\s]+")
.Where(word => !string.IsNullOrEmpty(word));

// Capitalize the first letter of each word and join them.
return string.Concat(words.Select(word => char.ToUpper(word[0]) + word.Substring(1).ToLower()));
}

public string GetValueType(JsonValue jsonValue, SerializableLanguage language)

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Debug)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'

Check warning on line 42 in src/Riverside.JsonBinder/Serialization/LanguageSerializer.cs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, Release)

Missing XML comment for publicly visible type or member 'LanguageSerializer.GetValueType(JsonValue, SerializableLanguage)'
{
if (language.Equals(SerializableLanguage.CSharp))
return jsonValue switch
{
JsonValue value when value.TryGetValue<int>(out _) => "int",
JsonValue value when value.TryGetValue<double>(out _) => "double",
JsonValue value when value.TryGetValue<string>(out _) => "string",
JsonValue value when value.TryGetValue<bool>(out _) => "bool",
_ => "object"
};
else if (language.Equals(SerializableLanguage.JavaScript) || language.Equals(SerializableLanguage.TypeScript))
return jsonValue switch
{
JsonValue value when value.TryGetValue<int>(out _) => "number",
JsonValue value when value.TryGetValue<double>(out _) => "number",
JsonValue value when value.TryGetValue<string>(out _) => "string",
JsonValue value when value.TryGetValue<bool>(out _) => "boolean",
_ => "any"
};
else if (language.Equals(SerializableLanguage.Java))
return jsonValue switch
{
JsonValue value when value.TryGetValue<int>(out _) => "int",
JsonValue value when value.TryGetValue<double>(out _) => "double",
JsonValue value when value.TryGetValue<string>(out _) => "String",
JsonValue value when value.TryGetValue<bool>(out _) => "boolean",
_ => "Object"
};
else if (language.Equals(SerializableLanguage.PHP))
return jsonValue switch
{
JsonValue value when value.TryGetValue<int>(out _) => "int",
JsonValue value when value.TryGetValue<double>(out _) => "float",
JsonValue value when value.TryGetValue<string>(out _) => "string",
JsonValue value when value.TryGetValue<bool>(out _) => "bool",
_ => "mixed" // PHP 8+ type hint
};
else if (language.Equals(SerializableLanguage.Python))
return jsonValue switch
{
JsonValue value when value.TryGetValue<int>(out _) => "int",
JsonValue value when value.TryGetValue<double>(out _) => "float",
JsonValue value when value.TryGetValue<string>(out _) => "str",
JsonValue value when value.TryGetValue<bool>(out _) => "bool",
_ => "object"
};
else if(language.Equals(SerializableLanguage.Ruby))
return jsonValue switch
{
JsonValue value when value.TryGetValue<int>(out _) => "Integer",
JsonValue value when value.TryGetValue<double>(out _) => "Float",
JsonValue value when value.TryGetValue<string>(out _) => "String",
JsonValue value when value.TryGetValue<bool>(out _) => "T::Boolean",
_ => "T.untyped"
};
else if (language.Equals(SerializableLanguage.Swift))
return jsonValue switch
{
JsonValue value when value.TryGetValue<int>(out _) => "Int",
JsonValue value when value.TryGetValue<double>(out _) => "Double",
JsonValue value when value.TryGetValue<string>(out _) => "String",
JsonValue value when value.TryGetValue<bool>(out _) => "Bool",
_ => "Any"
};

return "unknown";
}
}
Loading