Skip to content

Instantly share code, notes, and snippets.

@d-asensio
Created May 31, 2017 07:50
Show Gist options
  • Save d-asensio/bc12c027bafddf19167264d5291d9253 to your computer and use it in GitHub Desktop.
Save d-asensio/bc12c027bafddf19167264d5291d9253 to your computer and use it in GitHub Desktop.
@function valueToJSON($value, $key) {
$is_first_value: true;
$json_string: '{#{$key}:{';
@if (type-of($value) == 'map') {
$json_string: $json_string + '{';
@each $_key, $_value in $value {
@if not($is_first_value) {
$json_string: $json_string + ',';
}
$json_string: $json_string + '"#{$_key}": #{valueToJSON($_value)}';
$is_first_value: false;
}
$json_string: $json_string + '}';
} @else if(type-of($value) == 'list') {
$json_string: $json_string + '[';
@each $_value in $value {
@if not($is_first_value) {
$json_string: $json_string + ',';
}
$json_string: $json_string + '#{valueToJSON($_value)}';
$is_first_value: false;
}
$json_string: $json_string + ']';
} @else {
@if (type-of($value) == 'bool') {
$json_string: $value;
} @else {
$json_string: '"#{$value}"';
}
}
@return $json_string;
}
@d-asensio
Copy link
Author

d-asensio commented May 31, 2017

Not suitable for production ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment