# Zend_Config_Json

## Overview

JSON is an acronym for "JavaScript Object Notation"; while compatible with JavaScript, it is also intended as a general-purpose, cross-language data interchange format. Zend_Config_Json is a lightweight Zend_Config extension using JSON as its serialization format.

## Quick Start

The following is a JSON version of a standard application configuration.

    {
        "production":{
            "phpSettings":{
                "display_startup_errors": false,
                "display_errors": false
            },
            "includePaths":{
                "library": "APPLICATION_PATH/../library"
            },
            "bootstrap":{
                "path": "APPLICATION_PATH/Bootstrap.php",
                "class": "Bootstrap"
            },
            "appnamespace": "Application",,
            "resources":{
                "frontController":{
                    "controllerDirectory": "APPLICATION_PATH/controllers",
                    "moduleDirectory": "APPLICATION_PATH/modules",
                    "params":{
                        "displayExceptions": false
                    }
                },
                "modules":[],
                "db":{
                    "adapter": "pdo_sqlite",
                    "params":{
                        "dbname": "APPLICATION_PATH/../data/db/application.db"
                    }
                },
                "layout":{
                    "layoutPath": "APPLICATION_PATH/layouts/scripts/"
                }
            }
        },
        "staging":{
            "_extends": "production"
        },
        "testing":{
            "_extends": "production",
            "phpSettings":{
                "display_startup_errors": true,
                "display_errors": true
            },
        },
        "development":{
            "_extends": "production",
            "phpSettings":{
                "display_startup_errors": true,
                "display_errors": true
            },
            "resources":{
                "frontController":{
                    "params":{
                        "displayExceptions": true
                    }
                }
            }
        }
    }

To utilize it, you simply instantiate Zend_Config_Json, pointing it to the location of this file and indicating the section of the file to load. By default, constant names found in values will be substituted with their appropriate values.

    $config = new Zend_Config_Json(
        APPLICATION_PATH . '/configs/application.json',
        APPLICATION_ENV
    );

Once instantiated, you use it as you would any other configuration object.

    $db = Zend_Db::factory($config->resources->db);

Use Constants With Care

JSON has a strict structure with regards to data types. As such, you need to ensure that your constants are use correctly. For constants that have string values, put your constant values in double quotes (""). For non-string values, you can omit the quotes -- but be absolutely certain that they are not returning strings, as otherwise you will encounter parser errors with your configuration file. When in doubt, enclose the contant in double quotes.

## Configuration Options

The following options may be passed as keys to the third, `$options` argument of the constructor.

Zend_Config_Json Options

allow_modifications/allowModifications

  The default behavior of Zend_Config is to mark the object as immutable once loaded. Passing this flag with a boolean `true` will enable modifications to the object.

skip_extends/skipExtends

  By default, any time a section extends another, Zend_Config will merge the section with the section it extends. Speciying a boolean `true` value to this option will disable this feature, giving you only the configuration defined explicitly in that section.

ignore_constants

  By default, Zend_Config_Json will replace constant names found in values with the defined constant value. You map pass a boolean `true` to this option to disable this functionality.

  Please note that ignoring constants can potentially lead to parse errors, particularly if you are using constants for integer, float, or boolean values. The safest practice is to enclose constants within quotes.

## Available Methods

__construct $json, $section = null, $options = false

  Constructor. `$json` should be either a valid JSON string, or refer to a valid filesystem location containing a JSON configuration file. `$section`, if specified, indicates a specific section of the configuration file to use. `$options` is discussed in the options section.

setIgnoreConstants $flag

  This static function may be used to globally override the default settings for how constants found in JSON strings are handled. By default, constant names are replaced with the appropriate constant values; passing a boolean `true` value to this method will override that behavior. (You can override it per-instance via the `ignore_constants` option as well.)

ignoreConstants

  This static method gives you the current setting for the `ignore_constants` flag.