using System.Text.RegularExpressions;
public static void Main()
var json = "{ id: 1, cnj: \"1500312-82.2019.8.26.0050\" }";
var teste = JsonConvert.DeserializeObject<Teste>(json);
json = JsonConvert.SerializeObject(teste);
public int Id { get; set; }
public ConselhoNacionalJustica CNJ { get; set; }
public class ConselhoNacionalJustica
private const string NumeroMascara = @"{0:0000000}-{1:00}.{2:0000}.{3:0}.{4:00}.{5:0000}";
private static readonly Regex NumeroExpression = new Regex(@"(?<Numero>\d*)-(?<Digito>\d*).(?<Ano>\d*).(?<Orgao>\d*).(?<Tribunal>\d*).(?<Origem>\d*)");
public static implicit operator string(ConselhoNacionalJustica value) => value.ToString();
public static implicit operator ConselhoNacionalJustica(string value) => new ConselhoNacionalJustica(value);
public int Numero { get; set; }
public int Digito { get; set; }
public int Ano { get; set; }
public int Orgao { get; set; }
public int Tribunal { get; set; }
public int Origem { get; set; }
public ConselhoNacionalJustica() { }
private ConselhoNacionalJustica(string value): this()
if (NumeroExpression.IsMatch(value))
var match = NumeroExpression.Match(value).Groups;
this.Numero = int.Parse(match[nameof(Numero)].Value);
this.Digito = int.Parse(match[nameof(Digito)].Value);
this.Ano = int.Parse(match[nameof(Ano)].Value);
this.Orgao = int.Parse(match[nameof(Orgao)].Value);
this.Tribunal = int.Parse(match[nameof(Tribunal)].Value);
this.Origem = int.Parse(match[nameof(Origem)].Value);
public override string ToString()
return string.Format(NumeroMascara, Numero, Digito, Ano, Orgao, Tribunal, Origem);