Beispiel #1
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="invoiceObject">An JsonObject with invoice information</param>
 public Invoice(JsonObject invoiceObject)
     : base()
 {
     if (invoiceObject == null) throw new ArgumentNullException("invoiceObject");
     if (invoiceObject.Keys.Count <= 0) throw new ArgumentException("Not a vaild invoice object", "invoiceObject");
     this.LoadFromJSON(invoiceObject);
 }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="statsObject">An JsonObject with stats information</param>
 public SiteStatistics(JsonObject statsObject)
     : base()
 {
     if (statsObject == null) throw new ArgumentNullException("statsObject");
     if (statsObject.Keys.Count <= 0) throw new ArgumentException("Not a vaild stats object", "statsObject");
     this.LoadFromJSON(statsObject);
 }
 /// <summary>
 /// Method for getting a System.String from a JsonString
 /// </summary>
 /// <param name="jsonObject">The object to get a string from</param>
 /// <param name="fieldName">The field to retrieve</param>
 /// <param name="defaultValue">The default value to return if not found.</param>
 /// <returns>The string if applicable, defaultValue otherwise.</returns>
 public static string GetJsonStringAsString(JsonObject jsonObject, string fieldName, string defaultValue)
 {
     if (jsonObject.ContainsKey(fieldName) &&
         (jsonObject[fieldName] is JsonString))
         return ((JsonString)jsonObject[fieldName]).Value;
     else
         return defaultValue;
 }
        /// <summary>
        /// Method useful for validating that an object can be converted into a type
        /// </summary>
        /// <param name="jsonObject">The jsonObject to validate</param>
        /// <param name="fieldName">The field of the JsonObject to validate</param>
        /// <param name="fieldType">The type to try to validate against</param>
        public static void ValidateJsonField(JsonObject jsonObject, string fieldName, Type fieldType)
        {
            if (jsonObject == null)
                throw new NullReferenceException("jsonObject");

            if (jsonObject.ContainsKey(fieldName) == false)
                throw new JsonException(string.Format("Could not find key: '{0}' in JSON object", fieldName));

            if ((jsonObject[fieldName] == null) && (fieldType != null))
                throw new NullReferenceException(string.Format("jsonObject field '{0}' == NULL", fieldName));

            if (jsonObject[fieldName].GetType() != fieldType)
                throw new JsonException(string.Format("JSON field: '{0}', type is '{1}', but expecting: '{2}'", fieldName, jsonObject[fieldName].GetType(), fieldType));
        }
 private void LoadFromJSON(JsonObject obj)
 {
     // loop through the keys of this JsonObject to get stats info, and parse it out
     foreach (string key in obj.Keys)
     {
         switch (key)
         {
             case SellerNameKey:
                 _sellerName = obj.GetJSONContentAsString(key);
                 break;
             case SiteNameKey:
                 _siteName = obj.GetJSONContentAsString(key);
                 break;
             case StatsKey:
                 JsonObject statsObj = obj[key] as JsonObject;
                 foreach (string innerKey in statsObj.Keys)
                 {
                     switch (innerKey)
                     {
                         case TotalSubscriptionsKey:
                             _totalSubscriptions = statsObj.GetJSONContentAsInt(innerKey);
                             break;
                         case SubscriptionsTodayKey:
                             _subscriptionsToday = statsObj.GetJSONContentAsInt(innerKey);
                             break;
                         case TotalRevenueKey:
                             _totalRevenue = statsObj.GetJSONContentAsString(innerKey);
                             break;
                         case RevenueTodayKey:
                             _revenueToday = statsObj.GetJSONContentAsString(innerKey);
                             break;
                         case RevenueThisMonthKey:
                             _revenueThisMonth = statsObj.GetJSONContentAsString(innerKey);
                             break;
                         case RevenueThisYearKey:
                             _revenueThisYear = statsObj.GetJSONContentAsString(innerKey);
                             break;
                         default:
                             break;
                     }
                 }
                 break;
             default:
                 break;
         }
     }
 }
 private void LoadFromJSON(JsonObject obj)
 {
     foreach (string key in obj.Keys)
     {
         string theKey = key;
         if (key.Contains("_"))
             theKey = key.Replace("_", "-"); // Chargify seems to return different keys based on xml or json return type
         switch (theKey)
         {
             case TotalCountKey:
                 _totalCount = obj.GetJSONContentAsInt(key);
                 break;
             case CurrentPageKey:
                 _currentPage = obj.GetJSONContentAsInt(key);
                 break;
             case TotalPagesKey:
                 _totalPages = obj.GetJSONContentAsInt(key);
                 break;
             case PerPageKey:
                 _perPage = obj.GetJSONContentAsInt(key);
                 break;
             case MetadataKey:
                 _metadata = new List<IMetadata>();
                 JsonArray viewObj = obj[key] as JsonArray;
                 if (viewObj != null && viewObj.Items != null && viewObj.Length > 0)
                 {
                     foreach (JsonValue item in viewObj.Items)
                     {
                         var newObj = new Metadata();
                         var hasData = false;
                         var itemObj = (JsonObject)item;
                         foreach (string subItemKey in itemObj.Keys)
                         {
                             switch (subItemKey)
                             {
                                 case "resource_id":
                                     hasData = true;
                                     newObj.ResourceID = obj.GetJSONContentAsInt(subItemKey);
                                     break;
                                 case "name":
                                     hasData = true;
                                     newObj.Name = obj.GetJSONContentAsString(subItemKey);
                                     break;
                                 case "value":
                                     hasData = true;
                                     newObj.Value = obj.GetJSONContentAsString(subItemKey);
                                     break;
                                 default:
                                     break;
                             }
                         }
                         if (hasData)
                         {
                             _metadata.Add(newObj);
                         }
                     }
                 }
                 break;
             default:
                 break;
         }
     }
 }
 /// <summary>
 /// Constructor (json)
 /// </summary>
 /// <param name="MetadataResultObject"></param>
 public MetadataResult(JsonObject MetadataResultObject)
     : base()
 {
     if (MetadataResultObject == null)
         throw new ArgumentNullException("MetadataResultObject");
     if (MetadataResultObject.Keys.Count <= 0)
         throw new ArgumentException("Not a vaild metadata results object", "MetadataResultObject");
     LoadFromJSON(MetadataResultObject);
 }
        internal static JsonObject Parse(string str, ref int position)
        {
            JsonString.EatSpaces(str, ref position);

            if (position >= str.Length)
                return null;

            if (str[position] != '{')
                throw new JsonParseException(str, position);

            JsonObject jsonObject = new JsonObject();

            // Read all the pairs
            bool continueReading = true;

            // Read starting '{'
            position++;
            while (continueReading == true)
            {
                JsonString.EatSpaces(str, ref position);
                if (str[position] != '}')
                {
                    // Read string
                    JsonString jsonString = JsonString.Parse(str, ref position);
                    string key = jsonString.Value;

                    // Read seperator ':'
                    JsonString.EatSpaces(str, ref position);
                    if (str[position] != ':')
                        throw new JsonParseException(str, position);
                    position++;

                    // Read value
                    JsonValue value = ParseValue(str, ref position);

                    jsonObject.Add(key, value);
                }

                JsonString.EatSpaces(str, ref position);
                if (str[position] == '}')
                    continueReading = false;
                else if (str[position] != ',')
                    throw new JsonParseException(str, position);

                // Skip "," between pair of items
                position++;
            }

            return jsonObject;
        }
 /// <summary>
 /// Method for getting a JsonString as a System.String
 /// </summary>
 /// <param name="jsonObject">The object to get a string from</param>
 /// <param name="fieldName">The field to retrieve</param>
 /// <returns>The string if applicable, null otherwise.</returns>
 public static string GetJsonStringAsString(JsonObject jsonObject, string fieldName)
 {
     return GetJsonStringAsString(jsonObject, fieldName, null);
 }
Beispiel #10
0
 private void LoadFromJSON(JsonObject obj)
 {
     // loop through the keys of this JsonObject to get invoice info, and parse it out
     foreach (string key in obj.Keys)
     {
         switch (key)
         {
             case IDKey:
                 m_id = obj.GetJSONContentAsInt(key);
                 break;
             case SubscriptionIDKey:
                 _subscriptionID = obj.GetJSONContentAsInt(key);
                 break;
             case StatementIDKey:
                 _statementID = obj.GetJSONContentAsInt(key);
                 break;
             case SiteIDKey:
                 _siteID = obj.GetJSONContentAsInt(key);
                 break;
             case StateKey:
                 _state = obj.GetJSONContentAsSubscriptionState(key);
                 break;
             case TotalAmountInCentsKey:
                 _totalAmountInCents = obj.GetJSONContentAsInt(key);
                 break;
             case PaidAtKey:
                 _paidAt = obj.GetJSONContentAsDateTime(key);
                 break;
             case CreatedAtKey:
                 _createdAt = obj.GetJSONContentAsDateTime(key);
                 break;
             case UpdatedAtKey:
                 _updatedAt = obj.GetJSONContentAsDateTime(key);
                 break;
             case AmountDueInCentsKey:
                 _amountDueInCents = obj.GetJSONContentAsInt(key);
                 break;
             case NumberKey:
                 _number = obj.GetJSONContentAsString(key);
                 break;
             case ChargesKey:
                 _charges = new List<ICharge>();
                 JsonArray chargesArray = obj[key] as JsonArray;
                 if (chargesArray != null)
                 {
                     foreach (JsonObject charge in chargesArray.Items)
                     {
                         _charges.Add(charge.GetJSONContentAsCharge("charge"));
                     }
                 }
                 // Sanity check, should be equal.
                 if (chargesArray.Length != _charges.Count)
                 {
                     throw new JsonParseException(string.Format("Unable to parse charges ({0} != {1})", chargesArray.Length, _charges.Count));
                 }
                 break;
             case PaymentsAndCreditsKey:
                 break;
             default:
                 break;
         }
     }
 }