Chen
Chen

Reputation: 119

Should we use enums in web json result

For example, I have a web api which return a Json Http respone Body. The fields in JSON is meaningful, but the question is should I use a string to describe it ? or use a int enum?

Example A:

{
  "user_id": 123,
  "sex": "male",
  "status": "active"
}

Example B:

{
  "user_id": 123,
  "sex": 1, 
  "status": 1
}

which is better? and why?

maybe the Example can save some net flow?

Upvotes: 0

Views: 33

Answers (1)

Nimphious
Nimphious

Reputation: 5039

This depends on a couple of things, mainly: How many times do these values appear in the JSON you're sending, and are you using compression?

If you're compressing the data you're sending using gzip or something similar, then the difference will be negligible.

The best way to find out is to try both approaches for your use case and meter the data usage, and see which one works better for you.

Upvotes: 1

Related Questions