public void WriteAsJson(TextWriter output, string mode, ISerializationAnnotator annotator, bool pretty = false, int level = 0) { var jw = new JsonWriter(output,pretty:pretty,level:level); jw.OpenObject(); if (0 != Meta.Count) { jw.WriteProperty("meta",Meta); } if (null == Parent && null != Collectors && 0!=Collectors.Length) { jw.WriteProperty("collectors", Collectors.OfType<ICollector>().Select( c => new { key = c.Key, name = c.Name ?? c.Key, shortname = c.ShortName ?? c.Name ?? c.Key, group = c.Group, condition = c.Condition }.jsonify()).ToArray()); } if (null == Parent && null != Routs && 0 != Routs.Length) { jw.WriteProperty("routs", Routs.Select( c => new { key = c.Key ?? "nokey", name = c.Name ?? c.Key, shortname = c.ShortName ?? c.Name ?? c.Key, level = c.Level, parent = null == c.Parent ? "" : c.Parent.Key, }.jsonify()).ToArray()); } jw.WriteProperty("id",Id); jw.WriteProperty("isbucket",IsBucket); if (null != RouteKey) { jw.WriteProperty("key",RouteKey.Key); jw.WriteProperty("name",RouteKey.Name); jw.WriteProperty("sortkey",RouteKey.SortKey); jw.WriteProperty("comment",RouteKey.Comment); } jw.OpenProperty("values"); jw.OpenArray(); foreach (var value in Values) { jw.WriteObject(new {key=value.Key,value=value.Value}); } jw.CloseArray(); jw.CloseProperty(); if (null != _children && 0 != _children.Count) { jw.OpenProperty("aggs"); jw.OpenArray(); foreach (var node in Children) { jw.WriteObject(node.Value); } jw.CloseArray(); jw.CloseProperty(); } jw.CloseObject(); }
public static void WriteJson(IUser user, TextWriter output, string usermode = "", bool pretty = false, int level = 0) { if (string.IsNullOrWhiteSpace(usermode)) { usermode = "admin"; } var notnullonly = usermode != "store"; var jw = new JsonWriter(output,pretty:pretty,level:level); if (null == user) { jw.WriteValue(null); return; } jw.OpenObject(); jw.WriteProperty("__id", user.Id, notnullonly); jw.WriteProperty("__version", user.Version, notnullonly); jw.WriteProperty("createtime", user.CreateTime.ToUniversalTime(), notnullonly); jw.WriteProperty("updatetime", user.UpdateTime.ToUniversalTime(), notnullonly); if (usermode == "admin" || usermode == "store") { jw.WriteProperty("class", "user"); jw.WriteProperty("__type", "user"); var type = user.GetType(); jw.WriteProperty("netclass", type.FullName + ", " + type.Assembly.GetName().Name); } jw.WriteProperty("login", user.Login, notnullonly); jw.WriteProperty("name", user.Name, notnullonly); jw.WriteProperty("email", user.Email, notnullonly); jw.WriteProperty("admin", user.IsAdmin, notnullonly); jw.WriteProperty("isgroup", user.IsGroup, notnullonly); jw.WriteProperty("active", user.Active, notnullonly); jw.WriteProperty("expire", user.Expire.ToUniversalTime(), notnullonly); jw.WriteProperty("publickey", user.PublicKey, notnullonly); if (usermode == "store" || usermode == "admin") { jw.WriteProperty("hash", user.Hash, notnullonly); jw.WriteProperty("salt", user.Salt, notnullonly); jw.WriteProperty("resetkey", user.ResetKey, notnullonly); jw.WriteProperty("resetexpire", user.ResetExpire.ToUniversalTime(), notnullonly); jw.WriteProperty("logable", user.Logable, notnullonly); } jw.WriteProperty("domain", user.Domain, notnullonly); jw.WriteProperty("roles", user.Roles.OrderBy(_ => _).ToArray(), notnullonly); jw.WriteProperty("groups", user.Groups.OrderBy(_ => _).ToArray(), notnullonly); jw.WriteProperty("custom", user.Custom, notnullonly); var extensions = user as IJsonSerializationExtension; if (null != extensions) { extensions.WriteExtensions(jw, usermode, null); } jw.CloseObject(); }
public void WriteAsJson(TextWriter output, string mode, ISerializationAnnotator annotator, bool pretty = false, int level = 0) { if (string.IsNullOrWhiteSpace(mode)) { mode = "admin"; } var jw = new JsonWriter(output,pretty:pretty,level:level); jw.OpenObject(); jw.WriteProperty("name",Name); jw.WriteProperty("isauth",IsAuthenticated); jw.WriteProperty("authtype",AuthenticationType); jw.WriteProperty("isadmin",IsAdmin,true); jw.WriteProperty("isguest",IsGuest,true); jw.WriteProperty("iserror",IsError,true); jw.WriteProperty("state",State,true); jw.WriteProperty("stateinfo",StateInfo,true); if (null != Token) { jw.OpenProperty("token"); jw.WriteNative(Token.stringify(mode)); jw.CloseProperty(); } if (null != User) { jw.OpenProperty("user"); jw.WriteNative(User.stringify(mode)); jw.CloseProperty(); } jw.CloseObject(); }