public override object Deserialize(string value, Type t) { value = value.Trim(); if (value.StartsWith("[") && value.EndsWith("]")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] items = SophiaMapper.SplitByComma(value); IList en = (IList)Activator.CreateInstance(t); foreach (string s in items) { if (KeyType != null) { en.Add(KeyType.Deserialize(s, t.GetGenericArguments()[0])); } else { SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(t.GetGenericArguments()[0]); en.Add(sp.Deserialize(s, t.GetGenericArguments()[0])); } } return(en); }
public override object Deserialize(string value, Type t) { value = value.Trim(); if (value.StartsWith("{") && value.EndsWith("}")) { value = value.Substring(1, value.Length - 2).Trim(); } if (value.StartsWith("[") && value.EndsWith("]")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] dicsplits = SophiaMapper.SplitByComma(value); IDictionary dica = (IDictionary)Activator.CreateInstance(t); foreach (string s in dicsplits) { string h = s.Trim(); if (h.StartsWith("[") && h.EndsWith("]")) { h = h.Substring(1, h.Length - 2).Trim(); } string[] dta = SophiaMapper.SplitByComma(h); if (dta.Length != 2) { throw new ArgumentException($"Unable to parse dictionary item {s}"); } dica.Add(KeyType.Deserialize(dta[0].Trim(), t.GetGenericArguments()[0]), ValueType.Deserialize(dta[1].Trim(), t.GetGenericArguments()[1])); } return(dica); }
public override string Serialize(object o, Type t) { StringBuilder bld = new StringBuilder(); if (o is IEnumerable en) { bld.Append("["); bool add = false; foreach (object on in en) { add = true; if (KeyType != null) { bld.Append(KeyType.Serialize(on, t.GetGenericArguments()[0])); } else { SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(t.GetGenericArguments()[0]); bld.Append(sp.Serialize(on, t.GetGenericArguments()[0])); } bld.Append(", "); } if (add) { bld.Remove(bld.Length - 2, 2); } bld.Append("]"); return(bld.ToString()); } return(base.Serialize(o, t)); }
public async Task <InProgress <S> > AskAsync(T query, ulong fee = Constants.BaseConstants.ORACLE_QUERY_FEE, TTLType queryTtlType = TTLType.Delta, ulong queryTtl = Constants.BaseConstants.ORACLE_QUERY_TTL_VALUE, ulong responseRelativeTtl = Constants.BaseConstants.ORACLE_RESPONSE_TTL_VALUE, CancellationToken token = default(CancellationToken)) { Account.ValidatePrivateKey(); Account.Nonce++; await SignAndSendAsync(Account.Client.CreateOracleQueryTransaction(SophiaMapper.SerializeOracleClass(query), Account.KeyPair.PublicKey, Id, QueryFee, fee, queryTtlType, queryTtl, responseRelativeTtl, Account.Nonce, Account.Ttl), token).ConfigureAwait(false); QueryId = Encoding.EncodeQueryId(Account.KeyPair.PublicKey, Id, Account.Nonce); return(new InProgress <S>(new WaitForHash(this), new WaitForQueryId <T, S>())); }
public async Task <InProgress <bool> > RespondAsync(S answer, ulong respondTtl = Constants.BaseConstants.ORACLE_RESPONSE_TTL_VALUE, CancellationToken token = default(CancellationToken)) { Account.ValidatePrivateKey(); Account.Nonce++; string response = SophiaMapper.SerializeOracleClass(answer); await SignAndSendAsync(Account.Client.CreateOracleRespondTransaction(response, OracleId, Id, respondTtl, Fee, Account.Nonce, Account.Ttl), token).ConfigureAwait(false); return(new InProgress <bool>(new WaitForHash(this))); }
internal ContractReturn(ContractCallObject obj, string txinfo, Contract c) { CallerId = obj?.CallerId; Height = (long?)obj?.Height ?? -1; ContractId = obj?.ContractId; GasPrice = obj?.GasPrice ?? 0; GasUsed = obj?.GasUsed ?? 0; RawLog = obj?.Log.ToList() ?? new List <Event>(); Events = SophiaMapper.GetEvents(c, RawLog); TXInfo = txinfo; }
internal OracleQuestion(Account account, OracleQuery q) : base(account) { Ttl = q.Ttl; SenderNonce = q.SenderNonce; Fee = q.Fee; ResponseTtl = q.ResponseTtl; OracleId = q.OracleId; Id = q.Id; SenderId = q.SenderId; Query = SophiaMapper.DeserializeOracleClass <T>(Encoding.UTF8.GetString(Utils.Encoding.DecodeCheckWithIdentifier(q.Query))); }
public async Task <InProgress <OracleServer <T, S> > > RegisterOracleAsync <T, S>(ulong queryFee = Constants.BaseConstants.ORACLE_QUERY_FEE, ulong fee = Constants.BaseConstants.FEE, Ttl ttl = default(Ttl), ushort abiVersion = Constants.BaseConstants.ORACLE_VM_VERSION, CancellationToken token = default(CancellationToken)) { ValidatePrivateKey(); string queryformat = SophiaMapper.ClassToOracleFormat <T>(); string responseformat = SophiaMapper.ClassToOracleFormat <S>(); OracleServer <T, S> c = new OracleServer <T, S>(this); Nonce++; await c.SignAndSendAsync(Client.CreateOracleRegisterTransaction(queryformat, responseformat, KeyPair.PublicKey, queryFee, fee, ttl?.Type ?? TTLType.Delta, ttl?.Value ?? Constants.BaseConstants.ORACLE_TTL_VALUE, abiVersion, Nonce, Ttl), token).ConfigureAwait(false); return(new InProgress <OracleServer <T, S> >(new WaitForHash(c), new GetOracle <T, S>())); }
public override object Deserialize(string value, Type t) { if (!t.IsClass) { throw new ArgumentException("Record can be only classes"); } value = value.Trim(); int idx = value.IndexOf('{'); if (idx > -1) { value = value.Substring(idx); } if (value.StartsWith("{") && value.EndsWith("}")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] dicsplits = SophiaMapper.SplitByComma(value); List <(string, PropertyInfo)> props = OrderProps(t.GetTypeInfo()); object o = Activator.CreateInstance(t); foreach (string s in dicsplits) { string[] spl = SophiaMapper.SplitByTwoPoints(s); if (spl.Length != 2) { throw new ArgumentException($"Unable to record item {s}"); } string name = spl[0].Trim(); if (name.StartsWith("\"") && name.EndsWith("\"")) { name = name.Substring(1, name.Length - 2); } (string, PropertyInfo)prop = props.FirstOrDefault(a => a.Item1 == name); if (prop.Item1 == null) { throw new ArgumentException($"Unable to deserialize record, missing property {name} in class {t.Name}"); } if (!FieldTypes.ContainsKey(name)) { throw new ArgumentException($"Unable to deserialize record, missing property {name} in record definition {SophiaName}"); } SophiaType tp = FieldTypes[name]; object ob = tp.Deserialize(spl[1].Trim(), prop.Item2.PropertyType); prop.Item2.SetValue(o, ob); } return(o); }
public override object Deserialize(string value, Type t) { if (value.StartsWith("\"") && value.EndsWith("\"")) { value = value.Substring(1, value.Length - 2); } SophiaEventAttribute rec = t.GetCustomAttribute <SophiaEventAttribute>(); if (!t.IsClass) { throw new ArgumentException("Events can be only classes"); } if (rec == null) { throw new ArgumentException("Events classes should have the SophiaEvent Attribute"); } value = value.Trim(); int idx = value.IndexOf('('); if (idx > -1) { value = value.Substring(idx); } if (value.StartsWith("(") && value.EndsWith(")")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] dicsplits = SophiaMapper.SplitByComma(value); List <(string, PropertyInfo)> props = OrderProps(t.GetTypeInfo()); object o = Activator.CreateInstance(t); if (props.Count < FieldTypes.Count || dicsplits.Length != FieldTypes.Count) { throw new ArgumentException($"Unable to deserialize event, the class has less properties than the definition"); } for (int x = 0; x < FieldTypes.Count; x++) { PropertyInfo p = props[x].Item2; p.SetValue(o, FieldTypes[x].Deserialize(dicsplits[x], p.PropertyType)); } return(o); }
public override string Serialize(object o, Type t) { string fullname = t.FullName; if (fullname != null && fullname.StartsWith("System.ValueTuple")) { StringBuilder bld = new StringBuilder(); bld.Append("("); bool add = false; int x = 0; if (TupleTypes.Count > 0 && TupleTypes.Count != t.GetFields().Length) { throw new ArgumentException($"The object has {t.GetFields().Length} in the tuple, the sophia type has {TupleTypes.Count}"); } foreach (FieldInfo f in t.GetFields()) { add = true; if (TupleTypes.Count > 0) { bld.Append(TupleTypes[x].Serialize(f.GetValue(o), f.FieldType)); } else { SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(f.FieldType); bld.Append(sp.Serialize(f.GetValue(o), f.FieldType)); } bld.Append(", "); } if (add) { bld.Remove(bld.Length - 2, 2); } bld.Append(")"); return(bld.ToString()); } return(base.Serialize(o, t)); }
public override object Deserialize(string value, Type t) { string fullname = t.FullName; if (fullname != null && fullname.StartsWith("System.ValueTuple")) { value = value.Trim(); if (value.StartsWith("[") && value.EndsWith("]")) { value = value.Substring(1, value.Length - 2).Trim(); } string[] items = SophiaMapper.SplitByComma(value); if (TupleTypes.Count > 0 && TupleTypes.Count != items.Length) { throw new ArgumentException($"The object has {t.GetFields().Length} in the tuple, the sophia type has {items.Length}"); } List <object> objs = new List <object>(); for (int x = 0; x < items.Length; x++) { if (TupleTypes.Count > 0) { objs.Add(TupleTypes[x].Deserialize(items[x], t.GetGenericArguments()[x])); } else { SophiaType sp = SophiaMapper.GetSophiaPossibleTypeFromType(t.GetGenericArguments()[x]); objs.Add(sp.Deserialize(items[x], t.GetGenericArguments()[x])); } } MethodInfo method = typeof(ValueType).GetMethods(BindingFlags.Static | BindingFlags.Public).ToList().First(a => a.Name == "Create" && a.GetParameters().Length == objs.Count); return(method.Invoke(null, objs.ToArray())); } return(base.Deserialize(value, t)); }