Beispiel #1
0
        private void NormalizeMeasurements(Ingredient ingred)
        {
            if (!String.IsNullOrEmpty(ingred.measurement))
            {
                bool needsPluralized = false;
                double measurement;
                if (!String.IsNullOrEmpty(ingred.quantity) && double.TryParse(ingred.quantity, out measurement) && (measurement > 1))
                    needsPluralized = true;

                if (ingred.measurement.Equals("tsp", StringComparison.OrdinalIgnoreCase) ||
                    ingred.measurement.Equals("tsp.", StringComparison.OrdinalIgnoreCase))
                    ingred.measurement = needsPluralized ? "teaspoons" : "teaspoon";
                if (ingred.measurement.Equals("Tbsp", StringComparison.OrdinalIgnoreCase) ||
                    ingred.measurement.Equals("tbsp.", StringComparison.OrdinalIgnoreCase))
                    ingred.measurement = needsPluralized ? "tablespoons" : "tablespoon";

            }
        }
Beispiel #2
0
        private string ParseIngredientsToJSON(string p)
        {
            List<Ingredient> ingredients = new List<Ingredient>();
            //  What I have here is a mac NSDictionary.  I can't find a "real" C# parser for it, so
            //  I'm going to parse it by hand and then spit it back out as a parsable JSON string.
            using (StringReader reader = new StringReader(p))
            {
                Ingredient currentIngredient = new Ingredient();    // this is probably a waste of an object creation, but it saves me a lot of null-checking

                while (true)
                {
                    string thisLine = reader.ReadLine();
                    if (thisLine == null)
                        break;
                    thisLine = thisLine.Trim();
                    if (String.IsNullOrEmpty(thisLine))
                        continue;
                    if (thisLine.Equals("(") || thisLine.Equals("("))   // noop - first or last line
                        continue;
                    if (thisLine.Equals("{"))   //  new object
                    {
                        currentIngredient = new Ingredient();
                        continue;
                    }
                    if (thisLine.StartsWith("}"))   //  end of an object
                    {
                        NormalizeMeasurements(currentIngredient);
                        ingredients.Add(currentIngredient);
                        continue;
                    }
                    //  Now, split up the rest.
                    string[] pieces = thisLine.Split(new[] { " = " }, 10, StringSplitOptions.RemoveEmptyEntries);
                    if (!pieces.Any())
                        continue;

                    FillInField(pieces, currentIngredient);
                }
            }

            return JSONReaderWriter<List<Ingredient>>.WriteToString(ingredients);
        }
Beispiel #3
0
        private void FillInField(string[] pieces, Ingredient currentIngredient)
        {
            string fieldName = pieces[0].Trim();
            string value;

            if (pieces.Count() == 1)
                value = String.Empty;
            else if (pieces.Count() == 2)
                value = pieces[1].Trim().TrimEnd(';').Trim('\"');
            else
                value = String.Join(" = ", pieces, 1, pieces.Count() - 1).Trim().TrimEnd(';').Trim('\"');

            switch (fieldName)
            {
                case "name":
                    currentIngredient.name = value;
                    break;
                case "quantity":
                    currentIngredient.quantity = value;
                    break;
                case "method":
                    currentIngredient.method = value;
                    break;
                case "measurement":
                    currentIngredient.measurement = value;
                    break;
                case "isGroupTitle":
                    if (!value.Equals("NO", StringComparison.OrdinalIgnoreCase) && !value.Equals("0", StringComparison.OrdinalIgnoreCase))
                        currentIngredient.isGroupTitle = true;
                    break;
            }
        }