mirror of
https://github.com/graphql-python/graphene.git
synced 2025-04-25 12:03:41 +03:00
Remove duplicated test Remove unicode for python 3 Add more tests Add None case Update json type Remove unnecessary dependencies Add more test Add test
40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from __future__ import unicode_literals
|
|
|
|
from graphql.language.ast import (BooleanValue, FloatValue, IntValue,
|
|
StringValue, ListValue, ObjectValue)
|
|
|
|
from graphene.types.scalars import MIN_INT, MAX_INT
|
|
from .scalars import Scalar
|
|
|
|
|
|
class JSON(Scalar):
|
|
"""
|
|
The `JSON` scalar type represents JSON values as specified by
|
|
[ECMA-404](http://www.ecma-international.org/
|
|
publications/files/ECMA-ST/ECMA-404.pdf).
|
|
"""
|
|
|
|
@staticmethod
|
|
def identity(value):
|
|
return value
|
|
|
|
serialize = identity
|
|
parse_value = identity
|
|
|
|
@staticmethod
|
|
def parse_literal(ast):
|
|
if isinstance(ast, (StringValue, BooleanValue)):
|
|
return ast.value
|
|
elif isinstance(ast, IntValue):
|
|
num = int(ast.value)
|
|
if MIN_INT <= num <= MAX_INT:
|
|
return num
|
|
elif isinstance(ast, FloatValue):
|
|
return float(ast.value)
|
|
elif isinstance(ast, ListValue):
|
|
return [JSON.parse_literal(value) for value in ast.values]
|
|
elif isinstance(ast, ObjectValue):
|
|
return {field.name.value: JSON.parse_literal(field.value) for field in ast.fields}
|
|
else:
|
|
return None
|