128 lines
3.9 KiB
C#
128 lines
3.9 KiB
C#
// Copyright © 2010 Xamasoft
|
|
|
|
using Newtonsoft.Json;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
|
|
namespace Json2CSharpCodeGenerator.Lib;
|
|
|
|
public class FieldInfo
|
|
{
|
|
public FieldInfo(
|
|
IJsonClassGeneratorConfig generator,
|
|
string jsonMemberName,
|
|
JsonType type,
|
|
bool usePascalCase,
|
|
IReadOnlyList<object> examples
|
|
)
|
|
{
|
|
this._Generator = generator ?? throw new ArgumentNullException(nameof(generator));
|
|
JsonMemberName = jsonMemberName ?? throw new ArgumentNullException(nameof(jsonMemberName));
|
|
MemberName = jsonMemberName;
|
|
ContainsSpecialChars = IsContainsSpecialChars(MemberName);
|
|
|
|
if (usePascalCase || ContainsSpecialChars)
|
|
{
|
|
MemberName = JsonClassGenerator.ToTitleCase(MemberName);
|
|
}
|
|
|
|
Type = type ?? throw new ArgumentNullException(nameof(type));
|
|
Examples = examples ?? Array.Empty<object>();
|
|
}
|
|
|
|
private readonly IJsonClassGeneratorConfig _Generator;
|
|
|
|
/// <summary>Pascal-cased.</summary>
|
|
public string MemberName { get; }
|
|
/// <summary>Normally camelCased.</summary>
|
|
public string JsonMemberName { get; }
|
|
public JsonType Type { get; }
|
|
public IReadOnlyList<object> Examples { get; }
|
|
|
|
public bool ContainsSpecialChars { get; set; }
|
|
public int MyProperty { get; set; }
|
|
|
|
private static bool IsContainsSpecialChars(string text)
|
|
{
|
|
for (int i = 0; i < text.Length; i++)
|
|
{
|
|
char c = text[i];
|
|
if (!char.IsLetterOrDigit(c) && c != '_')
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
internal static string ToTitleCase(string text)
|
|
{
|
|
if (text is null)
|
|
throw new ArgumentNullException(nameof(text));
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
return text;
|
|
|
|
StringBuilder sb = new(text.Length);
|
|
bool lastCharWasSpecial = true;
|
|
|
|
for (int i = 0; i < text.Length; i++)
|
|
{
|
|
char c = text[i];
|
|
if (char.IsLetterOrDigit(c))
|
|
{
|
|
_ = sb.Append(lastCharWasSpecial ? char.ToUpper(c) : c);
|
|
lastCharWasSpecial = false;
|
|
}
|
|
else
|
|
{
|
|
lastCharWasSpecial = true;
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
public string GetGenerationCode(string jObject)
|
|
{
|
|
if (jObject is null)
|
|
throw new ArgumentNullException(nameof(jObject));
|
|
|
|
if (Type.Type == JsonTypeEnum.Array)
|
|
{
|
|
JsonType innermost = Type.GetInnermostType();
|
|
return string.Format(CultureInfo.InvariantCulture, "({1})JsonClassHelper.ReadArray<{5}>(JsonClassHelper.GetJToken<JArray>({0}, \"{2}\"), JsonClassHelper.{3}, typeof({6}))",
|
|
jObject,
|
|
Type.GetTypeName(),
|
|
JsonMemberName,
|
|
innermost.GetReaderName(),
|
|
-1,
|
|
innermost.GetTypeName(),
|
|
Type.GetTypeName()
|
|
);
|
|
}
|
|
else if (Type.Type == JsonTypeEnum.Dictionary)
|
|
{
|
|
return string.Format(CultureInfo.InvariantCulture, "({1})JsonClassHelper.ReadDictionary<{2}>(JsonClassHelper.GetJToken<JObject>({0}, \"{3}\"))",
|
|
jObject,
|
|
Type.GetTypeName(),
|
|
Type.InternalType.GetTypeName(),
|
|
JsonMemberName,
|
|
Type.GetTypeName()
|
|
);
|
|
}
|
|
else
|
|
{
|
|
return string.Format(CultureInfo.InvariantCulture, "JsonClassHelper.{1}(JsonClassHelper.GetJToken<{2}>({0}, \"{3}\"))",
|
|
jObject,
|
|
Type.GetReaderName(),
|
|
Type.GetJTokenType(),
|
|
JsonMemberName
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
public string GetExamplesText() => string.Join(separator: ", ", values: Examples.Take(5).Select(x => JsonConvert.SerializeObject(x)));
|
|
|
|
} |