Completed
Push — master ( ae9c65...55772a )
by Asmir
07:43 queued 05:39
created

JsonDeserializationVisitor::decode()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 22.9226

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 5
cts 16
cp 0.3125
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 7
nop 1
crap 22.9226
1
<?php
2
3
/*
4
 * Copyright 2016 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Serializer;
20
21
use JMS\Serializer\Exception\RuntimeException;
22
23
class JsonDeserializationVisitor extends GenericDeserializationVisitor
24
{
25 57
    protected function decode($str)
26
    {
27 57
        $decoded = json_decode($str, true);
28
29 57
        switch (json_last_error()) {
30 57
            case JSON_ERROR_NONE:
31 57
                return $decoded;
32
33
            case JSON_ERROR_DEPTH:
34
                throw new RuntimeException('Could not decode JSON, maximum stack depth exceeded.');
35
36
            case JSON_ERROR_STATE_MISMATCH:
37
                throw new RuntimeException('Could not decode JSON, underflow or the nodes mismatch.');
38
39
            case JSON_ERROR_CTRL_CHAR:
40
                throw new RuntimeException('Could not decode JSON, unexpected control character found.');
41
42
            case JSON_ERROR_SYNTAX:
43
                throw new RuntimeException('Could not decode JSON, syntax error - malformed JSON.');
44
45
            case JSON_ERROR_UTF8:
46
                throw new RuntimeException('Could not decode JSON, malformed UTF-8 characters (incorrectly encoded?)');
47
48
            default:
49
                throw new RuntimeException('Could not decode JSON.');
50
        }
51
    }
52
}
53