JSON Formatting और Indentation: Tabs vs Spaces Best Practices

2024-06-14

Formatting सिर्फ दिखावट नहीं है—consistent indentation reviews आसान बनाती है और merge noise घटाती है। यहां सही विकल्प चुनने का तरीका है.

JSON spec में whitespace

Parsers के लिए spaces और newlines semantic नहीं हैं; style human readability और team convention का मामला है.

Tabs बनाम spaces

Cross-editor consistency और साफ diffs के लिए spaces बेहतर हैं। Tabs legal हैं, पर public JSON examples में कम मिलते हैं.

2 बनाम 4 spaces

Front-end और Node ecosystem में 2 spaces आम हैं; कुछ Java/Python configs में 4 spaces दिखते हैं। एक repo में एक style रखें.

{
  "user": {
    "name": "Alice",
    "roles": ["admin", "editor"]
  }
}

Pretty-print कब करें

Debugging, documentation और manual edits में करें। Review से पहले formatting करने पर diff में असली बदलाव दिखते हैं.

Minify कब करें

Production transport/storage size के लिए minify करें। Editable source अलग रखें और minified JSON hand-edit न करें.

Team tips

JSON files के लिए .editorconfig उपयोग करें, optional CI parse check जोड़ें, और docs में consistent formatting रखें.