私はそのようなクラスを持っています:
[JsonObject(MemberSerialization.OptIn)]
public class foo
{
[JsonProperty("name_in_json")]
public string Bar { get; set; }
// etc.
public Dictionary<string, bool> ImageFlags { get; set; }
}
JSONは元々CSVファイルから生成され、各行はfooオブジェクトを表します。基本的にフラットなので、特定のキーをimageflagsにマップする必要があります。
ここの例に基づいてCustomCreationConverterを書いてみました。
これはフラグをうまくマップしているように見えますが、通常のプロパティの設定に失敗します-「name_in_json」の代わりに「bar」を探します。
foo型のオブジェクトの「name_in_json」値を取得するにはどうすればよいですか?
編集:
現在の解決策:
var custAttrs = objectType.GetProperties().Select(p => p.GetCustomAttributes(typeof(JsonPropertyAttribute), true)).ToArray();
var propNames = objectType.GetProperties().Select(p => p.Name.ToLower()).ToArray();
Dictionary<string, string> objProps = new Dictionary<string, string>();
for (int i = 0; i < propNames.Length; i++)
// not every property has json equivalent...
if (0 == custAttrs[i].Length)
{
continue;
}
var attr = custAttrs[i][0] as JsonPropertyAttribute;
objProps.Add(attr.PropertyName.ToLower(), propNames[i].ToLower());
}