コード例 #1
0
ファイル: UberBuilder.cs プロジェクト: poostwoud/discovery
    public string ToJsonString()
    {
        //*****
        if (Value != null && Data.Count > 0)
            throw new Exception("Value and sub-Data can't both be set for a Data element?");

        //***** Begin object;
        var json = new StringBuilder();
        json.Append("{");

        //***** Add properties;
        var first = true;
        if (!string.IsNullOrWhiteSpace(Id)) { json.Append(ToJsonAttribute("id", Id, first)); first = false; }
        if (!string.IsNullOrWhiteSpace(Name)) { json.Append(ToJsonAttribute("name", Name, first)); first = false; }
        if (!string.IsNullOrWhiteSpace(Rel)) { json.Append(ToJsonAttribute("rel", string.Format("[\"{0}\"], string.Join("\", \"", Rel.Split(new[] {" "}, StringSplitOptions.RemoveEmptyEntries))), first, true)); first = false; }
        if (Url != null && !string.IsNullOrWhiteSpace(Url.ToString())) { json.Append(ToJsonAttribute("url", Url.ToString(), first)); first = false; }
        if (Action != UberActions.NotSet) { json.Append(ToJsonAttribute("action", Action.ToString().ToLower(), first)); first = false; }
        if (Transclude != UberTransclusion.NotSet) { json.Append(ToJsonAttribute("transclude", Transclude.ToString().ToLower(), first)); first = false; }
        if (!string.IsNullOrWhiteSpace(Model)) { json.Append(ToJsonAttribute("model", Model, first)); first = false; }
        if (!string.IsNullOrWhiteSpace(Sending)) { json.Append(ToJsonAttribute("sending", Sending, first)); first = false; }
        if (!string.IsNullOrWhiteSpace(Accepting)) { json.Append(ToJsonAttribute("accepting", Accepting, first)); first = false; }

        //***** Close object, no Value and Data;
        if (Value == null && Data.Count == 0)
        {
            //***** Close object;
            json.Append("}");
            return json.ToString();
        }

        //***** Close object with value attribute;
        if (Value != null)
        {
            //***** Add value attribute;
            json.Append(ToJsonAttribute("value", Value, first));

            //***** Close object;
            json.Append("}");
            return json.ToString();
        }

        //***** Close object with data objects;
        json.Append(ToJsonAttribute("data", Data.ToJsonString(), first, true));
            
        //***** Close object;
        json.Append("}");
        return json.ToString();
    }

    #endregion //***** Methods
}
コード例 #2
0
ファイル: UberBuilder.cs プロジェクト: poostwoud/discovery
    public string ToXmlString()
    {
        //*****
        if (Value != null && Data.Count > 0)
            throw new Exception("Value and sub-Data can't both be set for a Data element?");

        //***** Begin element with attributes;
        var xml = new StringBuilder();
        xml.Append("<data");
        if (!string.IsNullOrWhiteSpace(Id)) xml.Append(ToXmlAttribute("id", Id));
        if (!string.IsNullOrWhiteSpace(Name)) xml.Append(ToXmlAttribute("name", Name));
        if (!string.IsNullOrWhiteSpace(Rel)) xml.Append(ToXmlAttribute("rel", Rel));
        if (Url != null && !string.IsNullOrWhiteSpace(Url.ToString())) xml.Append(ToXmlAttribute("url", Url.ToString()));
        if (Action != UberActions.NotSet) xml.Append(ToXmlAttribute("action", Action.ToString().ToLower()));
        if (Transclude != UberTransclusion.NotSet) xml.Append(ToXmlAttribute("transclude", Transclude.ToString().ToLower()));
        if (!string.IsNullOrWhiteSpace(Model)) xml.Append(ToXmlAttribute("model", Model));
        if (!string.IsNullOrWhiteSpace(Sending)) xml.Append(ToXmlAttribute("sending", Sending));
        if (!string.IsNullOrWhiteSpace(Accepting)) xml.Append(ToXmlAttribute("accepting", Accepting));

        //***** No Value and Data;
        if (Value == null && Data.Count == 0)
        {
            //***** Close element;
            xml.Append(" />");
            return xml.ToString();
        }

        //***** Value;
        if (Value != null)
        {
            //***** Close element with value;
            xml.AppendFormat(">{0}</data>", Value);
            return xml.ToString();
        }

        //***** Close element with data elements;
        xml.AppendFormat(">{0}</data>", Data.ToXmlString());
        return xml.ToString();
    }