/// <summary> /// /// </summary> /// <param name="iType"></param> /// <param name="sbDecode"></param> /// <param name="varName"></param> /// <param name="varCnt"></param> int DecodeSingle(Type iType, StringBuilder sbDecode, string varName) { if (iType == typeof(byte[])) { if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbDecode.Append("\r\nvar "); } else { sbDecode.Append("\r\n"); } sbDecode.Append($"{varName} = "); if (StandardTypes.STypes.TryGetValue(iType, out var tf)) { sbDecode.Append(tf.FGet); } sbDecode.Append(";"); } else if (iType.IsArray) { StringBuilder msb1 = new StringBuilder(); StringBuilder msb2 = new StringBuilder(); StringBuilder msb3 = new StringBuilder(); if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbDecode.Append("\r\nvar "); } else { sbDecode.Append("\r\n"); } for (int i = 0; i < iType.GetArrayRank(); i++) { if (i > 0) { msb1.Append(", "); msb3.Append(", "); } varCntTotal++; int ardpv = varCntTotal; msb1.Append("decoder.GetInt()"); msb3.Append($"ard{ardpv}_{i}"); //--- if (i == iType.GetArrayRank() - 1) {//last element msb2.Append($"\r\nfor(int ard{ardpv}_{i} = 0; ard{ardpv}_{i} < {varName}.GetLength({i}); ard{ardpv}_{i}++) {{"); varCntTotal++; UsedVars.Add($"{varName}[{msb3.ToString()}]"); DecodeSingle(iType.GetElementType(), msb2, $"{varName}[{msb3.ToString()}]"); msb2.Append($"\r\n}}"); } else { msb2.Append($"\r\nfor(int ard{ardpv}_{i} = 0; ard{ardpv}_{i} < {varName}.GetLength({i}); ard{ardpv}_{i}++)"); } } int iof = 0; string strf = StandardTypes.GetFriendlyName(iType); StringBuilder revArr = new StringBuilder(); for (int j = strf.Length - 1; j >= 0; j--) { var l = strf[j]; if (l == '[' || l == ']' || l == ',') { iof++; if (l == '[') { l = ']'; } else if (l == ']') { l = '['; } revArr.Append(l); } else { break; } } var revArrStr = revArr.ToString(); revArrStr = revArrStr.Substring(revArrStr.IndexOf("]") + 1); if (iof > 0) { strf = $"{strf.Substring(0, strf.Length - iof)}[{msb1.ToString()}]{revArrStr.ToString()}"; } else { strf = $"{strf}[{msb1.ToString()}]"; } sbDecode.Append($"{varName} = decoder.CheckNull() ? null : new {strf};"); sbDecode.Append($"\r\nif({varName} != null){{"); sbDecode.Append($"{msb2.ToString()}"); sbDecode.Append($"\r\n}}"); } else if (iType.GetInterface("ICollection`1") != null) { StringBuilder msb = new StringBuilder(); if (iType.GetInterface("ISet`1") != null || iType.GetInterface("IList`1") != null) { if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); msb.Append("\r\nvar "); } else { msb.Append("\r\n"); } msb.Append($"{varName} = decoder.CheckNull() ? null : new {StandardTypes.GetFriendlyName(iType)}();"); msb.Append($"\r\nif({varName} != null){{"); msb.Append($"\r\n\tdecoder.GetCollection(() => {{"); StringBuilder sbi = new StringBuilder(); varCntTotal++; int pv2 = varCntTotal; DecodeSingle(iType.GenericTypeArguments[0], sbi, $"pvar{pv2}"); msb.Append(sbi.ToString()); msb.Append($"\r\n\t\treturn pvar{pv2};"); msb.Append($"\r\n\t}}, {varName}, true);"); msb.Append($"\r\n}}"); sbDecode.Append(msb.ToString()); } else if (iType.GetInterface("IDictionary`2") != null) { if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); msb.Append("\r\nvar "); } else { msb.Append("\r\n"); } msb.Append($"{varName} = decoder.CheckNull() ? null : new {StandardTypes.GetFriendlyName(iType)}();"); msb.Append($"\r\nif({varName} != null){{"); msb.Append($"\r\n\tdecoder.GetCollection(() => {{"); StringBuilder sbi = new StringBuilder(); varCntTotal++; int pv2 = varCntTotal; DecodeSingle(iType.GenericTypeArguments[0], sbi, $"pvar{pv2}"); msb.Append(sbi.ToString()); msb.Append($"\r\n\t\treturn pvar{pv2};"); msb.Append($"\r\n}},"); msb.Append($"\r\n() => {{"); sbi.Clear(); varCntTotal++; int pv3 = varCntTotal; DecodeSingle(iType.GenericTypeArguments[1], sbi, $"pvar{pv3}"); msb.Append(sbi.ToString()); msb.Append($"\r\n\t\treturn pvar{pv3};"); msb.Append($"\r\n\t}}, {varName}, true);"); msb.Append($"\r\n}}"); sbDecode.Append(msb.ToString()); } } else if (iType.GetInterface("ITuple") != null) { Dictionary <int, Type> dTuple = new Dictionary <int, Type>(); StringBuilder sbi = new StringBuilder(); StringBuilder msb = new StringBuilder(); bool first = true; StringBuilder tupleType = new StringBuilder(); List <string> tuplSbi = new List <string>(); foreach (var gta in iType.GetGenericArguments()) { sbi.Clear(); varCntTotal++; int kv = varCntTotal; dTuple.Add(varCntTotal, gta); UsedVars.Add($"pvar{varCntTotal}"); DecodeSingle(gta, sbi, $"pvar{varCntTotal}"); if (first) { first = false; } else { tupleType.Append(", "); } tupleType.Append(StandardTypes.GetFriendlyName(gta)); var defaultValue = StandardTypes.GetDefaultValue(gta); if (defaultValue == null) { defaultValue = $"default({StandardTypes.GetFriendlyName(gta)})"; } msb.Append($"\r\n{StandardTypes.GetFriendlyName(gta)} pvar{kv} = {defaultValue};"); tuplSbi.Add(sbi.ToString()); } int elcnt = 0; foreach (var el in tuplSbi) { elcnt++; msb.Append(el); } if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); msb.Append("\r\nvar "); } else { msb.Append("\r\n"); } msb.Append($"{varName} = new Tuple<{tupleType.ToString()}>("); first = true; foreach (var el in dTuple) { if (first) { first = false; } else { msb.Append(", "); } msb.Append($"pvar{el.Key}"); } msb.Append(");"); sbDecode.Append(msb.ToString()); } else if (iType.IsEnum) { //var nullableEnum = Nullable.GetUnderlyingType(iType); Type underlyingType = Enum.GetUnderlyingType(iType); if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbDecode.Append("\r\nvar "); } else { sbDecode.Append("\r\n"); } sbDecode.Append($"{varName} = ({iType.Name})"); if (StandardTypes.STypes.TryGetValue(underlyingType, out var tf)) { sbDecode.Append(tf.FGet); } else { throw new Exception("Enum.GetUnderlyingType generation has failed"); } sbDecode.Append(";"); } else { if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbDecode.Append("\r\nvar "); } else { sbDecode.Append("\r\n"); } sbDecode.Append($"{varName} = "); if (StandardTypes.STypes.TryGetValue(iType, out var tf)) { sbDecode.Append(tf.FGet); } else { if (iType == myType) { throw new Exception("Cross-Reference exception. Object can't contain itself as a property"); } UsedObjects.Add(iType); sbDecode.Append(StandardTypes.GetFriendlyName(iType) + ".BiserDecode(null, decoder)"); } sbDecode.Append(";"); } return(varCntTotal); }
/// <summary> /// /// </summary> /// <param name="iType"></param> /// <param name="sbJsonDecode"></param> /// <param name="varName"></param> /// <param name="varCnt"></param> /// <param name="mapper"></param> void DecodeSingle(Type iType, StringBuilder sbJsonDecode, string varName) { if (iType == typeof(byte[])) { if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbJsonDecode.Append("\r\nvar "); } else { sbJsonDecode.Append("\r\n"); } sbJsonDecode.Append($"{varName} = "); if (StandardTypes.STypes.TryGetValue(iType, out var tf)) { sbJsonDecode.Append(tf.FGet); } sbJsonDecode.Append(";"); } else if (iType.IsArray) { StringBuilder msb1 = new StringBuilder(); StringBuilder msb2 = new StringBuilder(); StringBuilder msb3 = new StringBuilder(); var listType = typeof(List <>).MakeGenericType(iType.GetElementType()); if (iType.GetArrayRank() > 1) { var listType2 = typeof(Tuple <,>).MakeGenericType(typeof(List <int>), listType); varCntTotal++; int pv = varCntTotal; DecodeSingle(listType2, sbJsonDecode, $"pv{pv}"); //at this momemnt we can have Tuple //var pv1 = new Tuple<System.Collections.Generic.List<System.Int32>, System.Collections.Generic.List<System.Int32>>(pvar2, pvar5); for (int i = 0; i < iType.GetArrayRank(); i++) { if (i > 0) { msb1.Append(", "); } msb1.Append($"pv{pv}.Item1[{i}]"); } if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbJsonDecode.Append("\r\nvar "); } else { sbJsonDecode.Append("\r\n"); } int iof = 0; string strf = StandardTypes.GetFriendlyName(iType); StringBuilder revArr = new StringBuilder(); for (int j = strf.Length - 1; j >= 0; j--) { var l = strf[j]; if (l == '[' || l == ']' || l == ',') { iof++; if (l == '[') { l = ']'; } else if (l == ']') { l = '['; } revArr.Append(l); } else { break; } } var revArrStr = revArr.ToString(); revArrStr = revArrStr.Substring(revArrStr.IndexOf("]") + 1); if (iof > 0) { strf = $"{strf.Substring(0, strf.Length - iof)}[{msb1.ToString()}]{revArrStr.ToString()}"; } else { strf = $"{strf}[{msb1.ToString()}]"; } sbJsonDecode.Append($"{varName} = new {strf};"); varCntTotal++; int arenm = varCntTotal; sbJsonDecode.Append($"\r\nvar arenm{arenm} = pv{pv}.Item2.GetEnumerator();"); sbJsonDecode.Append($"\r\narenm{arenm}.MoveNext();"); varCntTotal++; int ardpv = varCntTotal; for (int i = 0; i < iType.GetArrayRank(); i++) { if (i > 0) { msb3.Append(", "); } msb3.Append($"ard{ardpv}_{i}"); if (i == iType.GetArrayRank() - 1) {//last element msb2.Append($"\r\nfor(int ard{ardpv}_{i} = 0; ard{ardpv}_{i} < {varName}.GetLength({i}); ard{ardpv}_{i}++) {{"); msb2.Append($"\r\n{varName}[{msb3.ToString()}] = arenm{arenm}.Current;"); msb2.Append($"\r\narenm{arenm}.MoveNext();"); msb2.Append($"\r\n}}"); } else { msb2.Append($"\r\nfor(int ard{ardpv}_{i} = 0; ard{ardpv}_{i} < {varName}.GetLength({i}); ard{ardpv}_{i}++)"); } } sbJsonDecode.Append(msb2.ToString()); } else { //Reading List to prepare transformation Dictionary <int, string> varmap = new Dictionary <int, string>(); for (int i = 0; i < iType.GetArrayRank(); i++) { // var listType = typeof(List<>).MakeGenericType(iType.GetElementType()); varCntTotal++; varmap.Add(i, $"intlst{varCntTotal}"); DecodeSingle(listType, sbJsonDecode, $"intlst{varCntTotal}"); } //sbJsonDecode.Append("\r\n//------------------"); if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbJsonDecode.Append("\r\nvar "); } else { sbJsonDecode.Append("\r\n"); } for (int i = 0; i < iType.GetArrayRank(); i++) { if (i > 0) { msb1.Append(", "); msb3.Append(", "); } varCntTotal++; int ardpv = varCntTotal; msb1.Append($"{varmap[i]}.Count"); msb3.Append($"ard{ardpv}_{i}"); //--- if (i == iType.GetArrayRank() - 1) {//last element msb2.Append($"\r\nfor(int ard{ardpv}_{i} = 0; ard{ardpv}_{i} < {varName}.GetLength({i}); ard{ardpv}_{i}++) {{"); //msb2.Append($"\r\n{varName}[ard{ardpv}_{i}] = {varmap[i]}[ard{ardpv}_{i}];"); msb2.Append($"\r\n{varName}[{msb3.ToString()}] = {varmap[i]}[ard{ardpv}_{i}];"); msb2.Append($"\r\n}}"); } else { msb2.Append($"\r\nfor(int ard{ardpv}_{i} = 0; ard{ardpv}_{i} < {varName}.GetLength({i}); ard{ardpv}_{i}++)"); } } int iof = 0; string strf = StandardTypes.GetFriendlyName(iType); StringBuilder revArr = new StringBuilder(); for (int j = strf.Length - 1; j >= 0; j--) { var l = strf[j]; if (l == '[' || l == ']' || l == ',') { iof++; if (l == '[') { l = ']'; } else if (l == ']') { l = '['; } revArr.Append(l); } else { break; } } var revArrStr = revArr.ToString(); revArrStr = revArrStr.Substring(revArrStr.IndexOf("]") + 1); if (iof > 0) { strf = $"{strf.Substring(0, strf.Length - iof)}[{msb1.ToString()}]{revArrStr.ToString()}"; } else { strf = $"{strf}[{msb1.ToString()}]"; } sbJsonDecode.Append($"{varName} = decoder.CheckNull() ? null : new {strf};"); sbJsonDecode.Append($"\r\nif({varName} != null){{"); sbJsonDecode.Append($"{msb2.ToString()}"); sbJsonDecode.Append($"\r\n}}"); } } else if (iType.GetInterface("ICollection`1") != null) { StringBuilder msb = new StringBuilder(); if (iType.GetInterface("ISet`1") != null || iType.GetInterface("IList`1") != null) { if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); msb.Append("\r\nvar "); } else { msb.Append("\r\n"); } msb.Append($"{varName} = decoder.CheckNull() ? null : new {StandardTypes.GetFriendlyName(iType)}();"); msb.Append($"\r\nif({varName} != null){{"); varCntTotal++; int pv1 = varCntTotal; msb.Append($"\r\n\tforeach(var el{pv1} in decoder.GetList()) {{"); StringBuilder sbi = new StringBuilder(); varCntTotal++; int pv2 = varCntTotal; DecodeSingle(iType.GenericTypeArguments[0], sbi, $"pvar{pv2}"); msb.Append(sbi.ToString()); msb.Append($"\r\n\t\t{varName}.Add(pvar{pv2});"); msb.Append($"\r\n\t}}"); msb.Append($"\r\n}}"); //eof if varname != null sbJsonDecode.Append(msb.ToString()); } else if (iType.GetInterface("IDictionary`2") != null) { var kT = iType.GenericTypeArguments[0]; if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); msb.Append("\r\nvar "); } else { msb.Append("\r\n"); } msb.Append($"{varName} = decoder.CheckNull() ? null : new {StandardTypes.GetFriendlyName(iType)}();"); msb.Append($"\r\nif({varName} != null){{"); varCntTotal++; int pv1 = varCntTotal; msb.Append($"\r\n\tforeach(var el{pv1} in decoder.GetDictionary<{kT}>()) {{"); StringBuilder sbi = new StringBuilder(); varCntTotal++; int pv2 = varCntTotal; DecodeSingle(iType.GenericTypeArguments[1], sbi, $"pvar{pv2}"); msb.Append(sbi.ToString()); msb.Append($"\r\n\t\t{varName}.Add(el{pv1}, pvar{pv2});"); msb.Append($"\r\n\t}}"); msb.Append($"\r\n}}"); //eof if varname != null sbJsonDecode.Append(msb.ToString()); } } else if (iType.GetInterface("ITuple") != null) { Dictionary <int, Type> dTuple = new Dictionary <int, Type>(); StringBuilder sbi = new StringBuilder(); StringBuilder msb = new StringBuilder(); bool first = true; StringBuilder tupleType = new StringBuilder(); List <string> tuplSbi = new List <string>(); foreach (var gta in iType.GetGenericArguments()) { sbi.Clear(); varCntTotal++; int ka = varCntTotal; dTuple.Add(varCntTotal, gta); UsedVars.Add($"pvar{varCntTotal}"); DecodeSingle(gta, sbi, $"pvar{varCntTotal}"); if (first) { first = false; } else { tupleType.Append(", "); } tupleType.Append(StandardTypes.GetFriendlyName(gta)); var defaultValue = StandardTypes.GetDefaultValue(gta); if (defaultValue == null) { defaultValue = $"default({StandardTypes.GetFriendlyName(gta)})"; } msb.Append($"\r\n{StandardTypes.GetFriendlyName(gta)} pvar{ka} = {defaultValue};"); //varCnt = varCntNew; tuplSbi.Add(sbi.ToString()); } varCntTotal++; msb.Append($"\r\nforeach (var tupleProps{varCntTotal} in decoder.GetDictionary<string>()){{"); msb.Append($"\r\nswitch(tupleProps{varCntTotal}){{"); int elcnt = 0; foreach (var el in tuplSbi) { elcnt++; msb.Append($"\r\ncase \"Item{elcnt}\":"); msb.Append(el); msb.Append($"\r\nbreak;"); } msb.Append("\r\n}}"); if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); msb.Append("\r\nvar "); } else { msb.Append("\r\n"); } msb.Append($"{varName} = new Tuple<{tupleType.ToString()}>("); first = true; foreach (var el in dTuple) { if (first) { first = false; } else { msb.Append(", "); } msb.Append($"pvar{el.Key}"); } msb.Append(");"); sbJsonDecode.Append(msb.ToString()); } else if (iType.IsEnum) { //var nullableEnum = Nullable.GetUnderlyingType(iType); Type underlyingType = Enum.GetUnderlyingType(iType); if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbJsonDecode.Append("\r\nvar "); } else { sbJsonDecode.Append("\r\n"); } sbJsonDecode.Append($"{varName} = ({iType.Name})"); if (StandardTypes.STypes.TryGetValue(underlyingType, out var tf)) { sbJsonDecode.Append(tf.FGet); } else { throw new Exception("Enum.GetUnderlyingType generation has failed"); } sbJsonDecode.Append(";"); } else { if (!UsedVars.Contains(varName)) { UsedVars.Add(varName); sbJsonDecode.Append("\r\nvar "); } else { sbJsonDecode.Append("\r\n"); } sbJsonDecode.Append($"{varName} = "); if (StandardTypes.STypes.TryGetValue(iType, out var tf)) { sbJsonDecode.Append(tf.FGet); } else { if (iType == myType) { throw new Exception("Cross-Reference exception. Object can't contain itself as a property"); } //adding object to UsedObjects list UsedObjects.Add(iType); sbJsonDecode.Append(StandardTypes.GetFriendlyName(iType) + ".BiserJsonDecode(null, decoder)"); } sbJsonDecode.Append(";"); } //return varCntTotal; }