Exemplo n.º 1
0
 public static void Merge(Json json)
 {
     json.Keys.ForEach(name => Merge(name, json[name]));
 }
Exemplo n.º 2
0
 public static void Merge(String name, Json json)
 {
     AppConfig.Merge(name, json);
 }
Exemplo n.º 3
0
 public static void Put(Json json)
 {
     json.Keys.ForEach(name => Put(name, json[name]));
 }
Exemplo n.º 4
0
 public static void Put(String name, Json json)
 {
     AppConfig.Put(name, json);
 }
Exemplo n.º 5
0
 public static dynamic LoadOrDefault(String uri, ICredentials credentials = null, Json args = null)
 {
     return LoadOrDefault(uri, null as Json, credentials, args);
 }
Exemplo n.º 6
0
 public static dynamic LoadOrDefault(String uri, Object @default, ICredentials credentials = null, Json args = null)
 {
     return LoadOrDefault(uri, () => @default, credentials, args);
 }
Exemplo n.º 7
0
 public static void Write(Stream s, Json json)
 {
     if (s == null || json == null) return;
     var s_json = json.ToCompactString();
     var b_json = Encoding.UTF8.GetBytes(s_json);
     s.Write(b_json, 0, b_json.Length);
 }
Exemplo n.º 8
0
 public static void Write(TextWriter w, Json json)
 {
     if (w == null || json == null) return;
     var s_json = json.ToCompactString();
     w.Write(s_json);
 }
Exemplo n.º 9
0
 public static dynamic Load(String uri, Json args, ICredentials credentials = null)
 {
     return Load(uri, credentials, args);
 }
Exemplo n.º 10
0
        public static dynamic Load(String uri, ICredentials credentials, Json args = null)
        {
            if (uri == null) return null;

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");
            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;
                req.Accept = "application/json,*/*";

                req.Method = args == null ? "GET" : "POST";
                if (args != null) req.GetRequestStream().WriteJson(args);

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString()));
                    }

                    var s_json = resp.GetResponseStream().DumpToString();
                    try { return Parse(s_json); }
                    catch (Exception ex)
                    {
                        throw new Exception(String.Format("Failed to parse JSON response from \"{1}\":{2}{0}{3}",
                            Environment.NewLine, uri, ex.Message, s_json));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString()), wex);
                    }
                    else
                    {
                        throw new Exception(String.Format("GET for \"{1}\" has failed: {0}{2}",
                            Environment.NewLine, uri, wex));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("GET for \"{1}\" has failed: {0}{2}",
                        Environment.NewLine, uri, ex));
                }
            }
            else
            {
                if (uri.StartsWith("file:///")) uri = uri.Slice("file:///".Length);

                var is_web = HttpContext.Current != null;
                var path = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path)) throw new Exception(String.Format(
                    "READ for \"{0}\" has failed: file \"{1}\" does not exist", uri, path));

                var s_json = File.ReadAllText(path);
                try { return Parse(s_json); }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("Failed to parse JSON from \"{1}\":{2}{0}{3}",
                        Environment.NewLine, uri, ex.Message, s_json));
                }
            }
        }
Exemplo n.º 11
0
        public static void Save(String uri, Json json, ICredentials credentials = null)
        {
            if (uri == null) return;

            var is_remote = uri.StartsWith("http://") || uri.StartsWith("https://");
            if (is_remote)
            {
                var req = (HttpWebRequest)WebRequest.Create(uri);
                req.Credentials = credentials ?? CredentialCache.DefaultCredentials;

                req.Method = json == null ? "DELETE" : "POST";
                if (json != null) new StreamWriter(req.GetRequestStream()).Write(json.ToCompactString());

                try
                {
                    var resp = (HttpWebResponse)req.GetResponse();
                    if (resp.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), req.Method));
                    }
                }
                catch (WebException wex)
                {
                    if (wex.Response != null)
                    {
                        var resp = (HttpWebResponse)wex.Response;
                        throw new Exception(String.Format("{4} for \"{1}\" has failed: {2}{0}{3}",
                            Environment.NewLine, uri, resp.StatusCode, resp.GetResponseStream().DumpToString(), wex, req.Method));
                    }
                    else
                    {
                        throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                            Environment.NewLine, uri, wex, req.Method));
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(String.Format("{3} for \"{1}\" has failed: {0}{2}",
                        Environment.NewLine, uri, ex, req.Method));
                }
            }
            else
            {
                if (uri.StartsWith("file:///")) uri = uri.Slice("file:///".Length);

                var is_web = HttpContext.Current != null;
                var path = is_web && uri.StartsWith("~") ? HttpContext.Current.Server.MapPath(uri) : uri;
                path = Path.GetFullPath(path);
                if (!File.Exists(path)) throw new Exception(String.Format(
                    "{2} for \"{0}\" has failed: file \"{1}\" does not exist", uri, path, json == null ? "DELETE" : "WRITE"));

                var s_json = json == null ? null : json.ToCompactString();
                if (s_json == null) File.Delete(path);
                else File.WriteAllText(path, s_json);
            }
        }
Exemplo n.º 12
0
 public static dynamic LoadOrDefault(String uri, Func<Object> @default, ICredentials credentials = null, Json args = null)
 {
     try { return Load(uri, credentials, args); }
     catch { return new Json((@default ?? (() => null))()); }
 }
Exemplo n.º 13
0
 public JsonDebugView(Json json, Object name) { _json = json; _name = name.ToInvariantString(); }
Exemplo n.º 14
0
 public JsonDebugView(Json json) { _json = json; }
Exemplo n.º 15
0
        // todo. when outputting HTML, also inject the "$(document).ready(function(){window.log.server(<actual log>});"
        private void Complete(Object result = null)
        {
            if (result == null) Native.End();

            var result_ex = result as Exception;
            if (result_ex != null)
            {
                Native.Clear();

                // note. why there's no line info in the stack trace?
                // see http://stackoverflow.com/questions/2673623/iis-not-giving-line-numbers-in-stack-trace-even-though-pdb-present
                var message = (CallStack.Enabled ? result_ex.ToString() : result_ex.Message).ToHtml();
                var trace = (Debug.Enabled ? Log.Message : null).ToHtml();

                var is_json = Native.ContentType == "application/json";
                if (is_json)
                {
                    this["Content-Type"] = "application/json";
                    var lowlevel_result = new Json(new { success = true, result = message, trace = trace });
                    Write(Hints.Prettyprint ? lowlevel_result.ToPrettyString() : lowlevel_result.ToCompactString());
                    Native.End();
                }
                else
                {
                    this["Content-Type"] = "text/html";
                    // note. trace will be written by Gateway.cs
                    // note. no need to dump exception, since it's already included into trace
                    Native.End();
                }
            }

            var result_json = result as Json;
            if (result_json != null)
            {
                (this.Text() == null || this.Text().IsEmpty()).AssertTrue();
                this["Content-Type"] = Hints.Prettyprint ? "text/html" : "application/json";
                var lowlevel_result = new Json(new { success = true, result = result_json, trace = (Debug.Enabled ? Log.Message : null).ToHtml() });
                Write(Hints.Prettyprint ? lowlevel_result.ToPrettyString() : lowlevel_result.ToCompactString());
                Native.End();
            }

            var result_string = result as String;
            if (result_string != null)
            {
                // todo. if this is HTML, inject log into it and show via javascript upon hotkey press
                (this.Text() == null || this.Text().IsEmpty()).AssertTrue();
                Write(result_string);
                Native.End();
            }

            var result_bytes = result as byte[];
            if (result_bytes != null)
            {
                (this.Bytes() == null || this.Bytes().IsEmpty()).AssertTrue();
                BinaryWrite(result_bytes);
                Native.End();
            }

            var result_stream = result as Stream;
            if (result_stream != null)
            {
                (this.Bytes() == null || this.Bytes().IsEmpty()).AssertTrue();
                StreamWrite(result_stream);
                Native.End();
            }

            throw AssertionHelper.Fail();
        }