Azure / azure-sdk-for-c

This repository is for active development of the Azure SDK for Embedded C. For consumers of the SDK we recommend visiting our versioned developer docs at https://azure.github.io/azure-sdk-for-c.
MIT License
226 stars 120 forks source link

[FEATURE REQ] Find JSON value by path #143

Closed gilbertw closed 5 years ago

gilbertw commented 5 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like A clear and concise description of what you want to happen.

Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.

Additional context Add any other context or screenshots about the feature request here.

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

sergey-shandar commented 5 years ago

Proposed API design

JSON value (az_json_value.h)

typedef enum {
  AZ_JSON_VALUE_NONE = 0,
  AZ_JSON_VALUE_NULL = 1,
  AZ_JSON_VALUE_BOOLEAN = 2,
  AZ_JSON_VALUE_NUMBER = 3,
  AZ_JSON_VALUE_STRING = 4,
  AZ_JSON_VALUE_OBJECT = 5,
  AZ_JSON_VALUE_ARRAY = 6,
} az_json_value_kind;

typedef struct {
  az_json_value_kind kind;
  union {
    bool boolean;
    az_span string;
    double number;
  } data;
} az_json_value;

az_result az_json_value_get_boolean(az_json_value * self, bool * out);
az_result az_json_value_get_number(az_json_value * self, double * out);
az_result az_json_value_get_string(az_json_value * self, az_span * out);

JSON pointer (az_json_pointer.h)

JSON pointer: https://tools.ietf.org/html/rfc6901

/**
 * Returns a next item in the JSON pointer.
 */
az_result az_json_pointer_parser_get(az_span_reader * json_pointer_parser, az_span * out);

JSON get by pointer (az_json_get.h)

az_result az_json_get_by_pointer(az_span json, az_span pointer, az_json_value * out);
JeffreyRichter commented 5 years ago

Yes I like this signature but I think the parameter names could be better. I'm questioning 'self' because it doesn't indicate that the first parameter must be a span over a json string. And, should ref be pointer to reflect the format of the string/span that must be passed here?

sergey-shandar commented 5 years ago

@JeffreyRichter Agree. I've changed the parameter names. Thank you for the feedback.

sergey-shandar commented 5 years ago

Proposed API design

JSON value (az_json_value.h)

typedef enum {
  AZ_JSON_VALUE_NONE = 0,
  AZ_JSON_VALUE_NULL = 1,
  AZ_JSON_VALUE_BOOLEAN = 2,
  AZ_JSON_VALUE_NUMBER = 3,
  AZ_JSON_VALUE_STRING = 4,
  AZ_JSON_VALUE_OBJECT = 5,
  AZ_JSON_VALUE_ARRAY = 6,
} az_json_value_kind;

typedef struct {
  az_json_value_kind kind;
  union {
    bool boolean;
    az_span string;
    double number;
  } data;
} az_json_value;

az_result az_json_value_get_boolean(az_json_value * self, bool * out);
az_result az_json_value_get_number(az_json_value * self, double * out);
az_result az_json_value_get_string(az_json_value * self, az_span * out);

JSON pointer (az_json_pointer_parser.h)

JSON pointer: https://tools.ietf.org/html/rfc6901

/**
 * Returns a next item in the JSON pointer.
 */
az_result az_span_reader_read_json_pointer_token(az_span_reader * self, az_span * out_pointer_token);

JSON get by pointer (az_json_get.h)

az_result az_json_get_by_pointer(az_span json, az_span pointer, az_json_value * out);
sergey-shandar commented 5 years ago

Merged.