From 1c7ae192bb93fbd541704c754284a439f77f24ae Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 14 Feb 2025 08:55:26 +0100 Subject: [PATCH] Switch from just saving XML data to parsing JSON data --- json.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ json.h | 12 ++++++ 2 files changed, 142 insertions(+) create mode 100644 json.c create mode 100644 json.h diff --git a/json.c b/json.c new file mode 100644 index 0000000..1b3b943 --- /dev/null +++ b/json.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include + +#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; +} diff --git a/json.h b/json.h new file mode 100644 index 0000000..b04170b --- /dev/null +++ b/json.h @@ -0,0 +1,12 @@ +/* + This software is licensed under GPL-3, as shown in the file LICENSE + Author: Linux Gruppe IRB + Copyright: Linux Gruppe IRB, 2025 +*/ + +#ifndef _JSON_H +#define _JSON_H + +char *json2txt(char *json); + +#endif