JSON Response parsing

PassioSearchNutritionPreview

  • Holds macro nutrient information for the default serving size of a database reference object

  • Mapping function to a PassioSearchNutritionPreview object, where the NutritionPreviewResult is the root JSON object:

internal fun NutritionPreviewResult.toDataModel(): PassioSearchNutritionPreview {
    val servingUnit = portion.name ?: ""
    val servingQuantity = if (portion.suggestedQuantity != null) {
        portion.suggestedQuantity!!.first()
    } else {
        portion.quantity ?: 0.0
    }
    val weightUnit = portion.weight?.unit ?: ""
    val weightQuantity = (portion.weight?.value ?: 0.0) * servingQuantity

    return PassioSearchNutritionPreview(
        calories.roundToInt(),
        carbs,
        protein,
        fat,
        fiber,
        servingUnit,
        servingQuantity,
        weightUnit,
        weightQuantity
    )
}

PassioFoodDataInfo

  • SDK representation of the database reference object

  • Does not contain all of the nutritional data, but has reference fields that can be used to fetch the full data

  • Mapping function to a PassioFoodDataInfo object from a list of JSON objects, where the response.results represents an array of JSON objects:

PassioFoodItem

  • Contains all of the nutritional information for a single database entry

  • Top-level object that contains one or more ingredients

Parsing from search routes

  • Typically used in the SDK when fetching from a reference PassioFoodDataInfoobject

  • Mapping function that creates a PassioFoodItem, using a ResponseFoodItem that represents a top level JSON object, and parsing functions for PassioIngredientandPassioFoodAmount:

Parsing from packaged product routes

  • Used in the SDK when fetching from a productCode, usually during barcode scanning

  • Mapping function that creates a PassioFoodItem, using a ResponseFoodItem that represents a top level JSON object, and parsing functions for PassioIngredientandPassioFoodAmount:

PassioIngredient

  • Contains nutritional and serving size data for a single food item

  • Mapping function that creates a PassioIngredient, using a ResponseIngredient that represents a top level JSON object, and parsing functions for PassioNutrients, PassioFoodAmount and PassioFoodMetadata:

PassioFoodAmount

  • SDK representation of serving portions, including serving quantities, units and their weights

  • If there are no portions in the JSON response, the SDK manually creates a 100 grams serving size

  • If the PassioFoodItem contains more then one ingredient, and there are no portions in the JSON response, the SDK will manually create a serving size called "Serving" , with it's weight being the summed up weight of all of the ingredients

  • The SDK offers conversion between units cup, teaspoon and tablespoon. If the JSON response contains only one or two of these units, the SDK will calculate the remaning ones and add them to the list of serving sizes

  • Mapping function that creates a PassioFoodAmount, using a ResponsePortions that represents a top level JSON object:

PassioNutrients

  • Data class that contains all of the macro and micro nutrients supported by the Passio Nutritional database

  • The SDK parses the values for Calories and VitaminA differently from other nutrients. Calories are parsed as UnitEnergy, and VitaminA has a Double value

  • The nutrientDefaults serves as a helper list that references the nutrient field, it's JSON "id" and JSON "shortName"

  • Mapping function that creates a PassioNutrients object, using a list of ResponseNutrient objects that represent a top level JSON array

PassioFoodMetadata

  • Contains miscellaneous data about the food ingredient like it's data source, product code, tags and ingredient description

  • Mapping function that creates a PassioFoodMetadata, using a ResponseIngredient that represents a top level JSON object:

PassioAdvisorFoodInfo

  • Represents a object returned by Passio's LLM services

  • Mapping function that creates a list of PassioAdvisorFoodInfo objects, using a json String as a top level JSON object:

PassioSpeechRecognitionModel

  • Represents a result from the speech recognition service

  • Mapping function that creates a list of PassioSpeechRecognitionModel objects, using a result String as a top level JSON array, also usign PassioFoodDataInfo and PassioAdvisorFoodInfomapping:

Last updated