1#!/usr/bin/env python 2 3# 4# Copyright (C) 2024 The Android Open Source Project 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"""A custom json parser that additionally supports line comments.""" 20 21import json 22import re 23 24# RegEx of removing line comment line in JSON. 25_LINE_COMMENT_RE = re.compile(r'\/\/[^\n\r]*[\n\r]') 26 27 28def _load_json_with_comment(json_str: str): 29 """Parse JSON string with accepting line comment.""" 30 raw_text = re.sub(_LINE_COMMENT_RE, '', json_str) 31 return json.loads(raw_text) 32