Switch from just saving XML data to parsing JSON data
This commit is contained in:
parent
f49f7731aa
commit
1c7ae192bb
|
@ -0,0 +1,130 @@
|
|||
#include <cjson/cJSON.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "json.h"
|
||||
|
||||
char *json2txt(char *json)
|
||||
{
|
||||
const cJSON *personalDataJSON = NULL;
|
||||
const cJSON *documentTypeJSON = NULL;
|
||||
const cJSON *issuingStateJSON = NULL;
|
||||
const cJSON *dateOfExpiryJSON = NULL;
|
||||
const cJSON *givenNamesJSON = NULL;
|
||||
const cJSON *familyNamesJSON = NULL;
|
||||
const cJSON *artisticNameJSON = NULL;
|
||||
const cJSON *academicTitleJSON = NULL;
|
||||
const cJSON *placeOfBirthJSON = NULL;
|
||||
const cJSON *nationalityJSON = NULL;
|
||||
const cJSON *birthNameJSON = NULL;
|
||||
const cJSON *streetJSON = NULL;
|
||||
const cJSON *cityJSON = NULL;
|
||||
const cJSON *countryJSON = NULL;
|
||||
const cJSON *zipCodeJSON = NULL;
|
||||
const cJSON *residencePermitJSON = NULL;
|
||||
const cJSON *dateOfBirthJSON = NULL;
|
||||
char *ret;
|
||||
|
||||
cJSON *eidJSON = cJSON_Parse(json);
|
||||
|
||||
if (eidJSON == NULL)
|
||||
{
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
|
||||
if (error_ptr != NULL)
|
||||
{
|
||||
fprintf(stderr, "Error before: %s\n", error_ptr);
|
||||
}
|
||||
|
||||
ret = NULL;
|
||||
goto end;
|
||||
}
|
||||
|
||||
personalDataJSON = cJSON_GetObjectItemCaseSensitive(eidJSON, "PersonalData");
|
||||
|
||||
if (personalDataJSON == NULL)
|
||||
{
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
|
||||
if (error_ptr != NULL)
|
||||
{
|
||||
fprintf(stderr, "Error before: %s\n", error_ptr);
|
||||
}
|
||||
|
||||
ret = NULL;;
|
||||
goto end;
|
||||
}
|
||||
|
||||
documentTypeJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "DocumentType");
|
||||
issuingStateJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "IssuingState");
|
||||
dateOfExpiryJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "DateOfExpiry");
|
||||
givenNamesJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "GivenNames");
|
||||
familyNamesJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "FamilyNames");
|
||||
artisticNameJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "ArtisticName");
|
||||
academicTitleJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "AcademicTitle");
|
||||
placeOfBirthJSON = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(personalDataJSON, "PlaceOfBirth"), "FreetextPlace");
|
||||
nationalityJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "Nationality");
|
||||
birthNameJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "BirthName");
|
||||
streetJSON = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(personalDataJSON, "PlaceOfResidence"), "Structured Place"), "Street");
|
||||
cityJSON = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(personalDataJSON, "PlaceOfResidence"), "Structured Place"), "City");
|
||||
countryJSON = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(personalDataJSON, "PlaceOfResidence"), "Structured Place"), "Country");
|
||||
zipCodeJSON = cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(cJSON_GetObjectItemCaseSensitive(personalDataJSON, "PlaceOfResidence"), "Structured Place"), "ZipCode");
|
||||
residencePermitJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "ResidencePermit");
|
||||
dateOfBirthJSON = cJSON_GetObjectItemCaseSensitive(personalDataJSON, "DateOfBirth");
|
||||
|
||||
if ((ret = (char *) calloc(1024, sizeof(char))) == NULL)
|
||||
goto end;
|
||||
|
||||
if (cJSON_IsString(documentTypeJSON) && (documentTypeJSON->valuestring != NULL))
|
||||
sprintf(ret, "Document type: %s\n", documentTypeJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(issuingStateJSON) && (issuingStateJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Issuing state: %s\n", issuingStateJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(dateOfExpiryJSON) && (dateOfExpiryJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Date of expiry: %s\n", dateOfExpiryJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(givenNamesJSON) && (givenNamesJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Given names: %s\n", givenNamesJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(familyNamesJSON) && (familyNamesJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Family names: %s\n", familyNamesJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(artisticNameJSON) && (artisticNameJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Artistic name: %s\n", artisticNameJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(academicTitleJSON) && (academicTitleJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Academic title: %s\n", academicTitleJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(placeOfBirthJSON) && (placeOfBirthJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Place of birth: %s\n", placeOfBirthJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(nationalityJSON) && (nationalityJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Nationality: %s\n", nationalityJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(birthNameJSON) && (birthNameJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Birth name: %s\n", birthNameJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(streetJSON) && (streetJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Street: %s\n", streetJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(cityJSON) && (cityJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "City: %s\n", cityJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(countryJSON) && (countryJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Country: %s\n", countryJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(zipCodeJSON) && (zipCodeJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Zip code: %s\n", zipCodeJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(residencePermitJSON) && (residencePermitJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Residence permit: %s\n", residencePermitJSON->valuestring);
|
||||
|
||||
if (cJSON_IsString(dateOfBirthJSON) && (dateOfBirthJSON->valuestring != NULL))
|
||||
sprintf(ret + strlen(ret), "Date of birth: %s\n", dateOfBirthJSON->valuestring);
|
||||
|
||||
end:
|
||||
cJSON_Delete(eidJSON);
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue