Is it advisable to include both the numerical value and the string representation of an enum in my API design?

Context

In the development of a product, I am faced with managing various enumerated values to categorize different data types. This is particularly relevant in C programming for a microcontroller environment:

typedef enum
{
    Temperature     = 100,
    InputVoltage    = 200,
    AverageVoltage  = 201,
    Current         = 300,
} ReadingType_t;

This approach enables us to handle "reading" data in a generic manner, as illustrated below:

typedef struct
{
    ReadingType_t type;
    sint32_t      value;
} Reading_t;

Reading_t readings[5];
readings[0].type = Temperature;
readings[0].value = 25; // in degrees C
readings[1].type = InputVoltage;
readings[1].value = 4321; // in milliVolts

Challenge

My current task involves defining a JSON-based API for displaying information structured like this. Several potential formats are being considered:

readings = [
    {
        "type": "Temperature",
        "value": 25
    },
    {
        "type": "InputVoltage",
        "value": 4321,
    }
]

or

readings = [
    {
        "type": 100,
        "value": 25
    },
    {
        "type": 200,
        "value": 4321,
    }
]

or

readings = [
    {
        "type": {
            "code": 100,
            "name": "Temperature"
        },
        "value": 25
    },
    {
        "type": {
            "code": 200,
            "name": "InputVoltage"
        },
        "value": 4321,
    }
]

Assumptions

It is assumed that the API will produce JSON output.

The API is expected to be utilized by a JavaScript client application and/or directly accessed for debugging purposes.

Key Questions

My inquiries revolve around the following:

  1. Should both the enumeration "code" and its corresponding "name" be included? Is there a standard convention for this dilemma?
  2. If including the "name," should it follow a conventional language format? For instance, should it be "InputVoltage" or "Input Voltage"?
  3. How can support for Internationalization be balanced with user-friendliness? For example:
    • Emphasizing the numeric "code" would eliminate ambiguity regarding the data type, but require a lookup table for converting to human-readable strings (potentially necessitating separate tables for internationalization).
    • Sending the "name" in natural language would delegate internationalization responsibilities to the API (which may be acceptable), but hinder logical operations based on the value (as the "name" varies based on language while the code remains constant).

Answer №1

It is not necessary to include both the code and the name in the output. Doing so can create unnecessary dependency on specific implementations.

Instead, I suggest including only the name in the output file. While the code may change over time, the name is likely to remain constant and provide more meaningful information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Fill in the text box based on the selected option from the dropdown menu

My HTML code snippet is as follows: <select name="plot_no" id="plot_no" class="dropdown validate_B"> <option value="">Select number of Plots to book</option> <option value="1">1</option> <opti ...

What could be the reason Angular is not refreshing with a JSON file update?

Having trouble using a basic Angular JS app to load data from a JSON file onto a website. The JSON file contains: {"a": "a"} The Angular application setup is as follows: var app = angular.module("app", []) .controller("ctrl", ["ser", function(ser) { ...

The navigation bar fails to respond when clicked on mobile devices

Calling all developers! I need a helping hand to tackle an issue that's holding me back from completing this website. Can someone please take a look at on their smartphone browser and figure out why the responsive menu icon isn't working when cl ...

Having trouble accessing the grandchild value in ReactJs

I'm having trouble accessing the grandchild value from a local JSON file. It works fine on all other pages, but when I use useEffect, I can't retrieve the value. I am able to access the child object value, but not the grandchild value. import Rea ...

Unable to display any content from JSON data in Spinner despite no errors being present

It seems like the issue I'm encountering is due to the UI drawing my layout first, which includes a spinner before fetching data from the backend. I need to figure out how to block it and wait for the data retrieval process to complete before populati ...

Creating a dynamic duplication feature for a form's text area

As a newcomer to the world of Javascript, I am embarking on creating a review-writing page. However, I have hit a roadblock in my progress. The main issue I am encountering is implementing a button that allows users to add a new section by duplicating an ...

Utilize -fsanitize=address with Clang on a Windows environment

My goal is to utilize Clang instead of Valgrind on Windows for detecting buffer overflows, dynamic memory misuse, and other issues in C/C++ programs that I have developed. I managed to successfully compile Clang by following the guidance provided here. Wh ...

Can the static keyword be omitted in a function definition even if it was present in the function declaration

In my quest to create a static function, I encountered an interesting scenario where the placement of the "static" keyword seemed to have no impact on compiler warnings: //file a.c version 1 static int foo(); ... static int foo() { ... } Surprisingly, it ...

Strategies for sending data to child components in Vue

Within my parent component, I am making an API call and receiving a response. My goal is to pass this response as a prop to a child component in Vue. Below is the snippet of the parent component and the API call: <button class="btn button col-2&quo ...

The sub-menu is being obscured by a PDF element in Internet Explorer, causing visibility issues

I have an object type tag on my page that displays a PDF file. Everything is functioning correctly, but in Internet Explorer, my sub-menu is being hidden behind the object tag. var objTag = $('<object></object>') .attr({ data: source ...

Exploring the World of Wordpress Plugins and Navigating the wp-json API

Seeking guidance on a project I'm working on. I am currently developing a WordPress plugin that will be available to the public for free. This plugin is designed to enable bloggers to incorporate a definition widget using Bootstrap's data-toggle ...

Searching through multiple columns in datatables

I encountered an issue with searching multiple columns in my serverside datatables. Individual column searches work fine, but when trying to search on more than one column simultaneously, the filter does not function as expected. For example, searching by ...

Tips for preserving the drag and drop sequence of various elements (images, buttons, text, paragraphs) using PHP and MySQL

Is there a way to retain the position of drag and drop elements (such as images, buttons, text, paragraphs) in php and mysql? ...

Erase a Pair of JS Code Lines from the Start of Each Wordpress Document

My website keeps getting redirected to a malware site and after investigating, I found a suspicious code at the beginning of each page. Here is an example - var gdjfgjfgj235f = 1; var d=document;var s=d.createElement('script'); s.type='tex ...

The GET parameter is not being passed in the AJAX request

Despite my efforts, I am struggling to successfully send a variable using the AJAX GET method. The PHP file consistently returns 3, indicating that the variable 'q' was not received in the PHP file. <script type="text/javascript"> func ...

Logic for iterating through blocks in JavaScript

I have a code that consists of an array containing multiple objects, in this case URLs. I am currently using a loop to iterate through this array and send a request for each URL. However, I am looking for a way to optimize this process by breaking it dow ...

Modifying the display style of error messages in Laravel 5 JSON: A guide

I'm attempting to retrieve Laravel 5 error messages and display them in the error div using AJAX. However, the problem arises when Laravel returns error messages enclosed in square brackets which cannot be accessed. In AJAX, I am able to access succe ...

Is there a way to trim an image in react js once it has been uploaded?

Is there a way to crop an image in react js after uploading? Hello everyone! I need to crop or edit an image after uploading it, but I'm not sure how to do it. I want to maintain a constant aspect ratio while cropping. I attempted to use react-easy-c ...

Accessing template attribute value in AngularJS controllerIn AngularJS, retrieving the

I am new to using angularjs version 1.6.4 and I have implemented the module leon/angular-upload for file upload functionality in minify version. After a successful upload, the server returns a JSON object containing information about the uploaded file in t ...

"Engaging with the touchscreen inhibits the triggering of click

Within this div, I have implemented touch-action:pan-y;. Surrounding this div is an anchor tag. If you click on the div, the link will successfully redirect. However, if you swipe on the div and then click, the link won't work on the first attempt bu ...